一 屬性注入介紹
1 創建對象時候,向類里面屬性設置值。
2 Java設置屬性的三種方法
(1) 使用set方法注入
public class User {
private String name;
public void setName(String name){
this.name = name;
}
}
User user = new User();
user.setName("abcd");
(2) 有參構造注入
public class User {
private String name;
public User(String name){
this.name = name;
}
}
User user = new User("Jack");
(3) 使用接口注入
public interface User {
public void delete(String name);
}
public class UserImpl implements User{
private String name;
public void delete(String name) {
this.name = name;
}
}
3 在Spring框架里面,支持前兩種方式
(1)set方法注入(重點)
(2)有參構造注入
二 Spring框架屬性注入
1 有參構造注入
(1) 有參構造
package IOC;
/**
* Created by pc on 2017/9/9.
*/
public class PropertyDemo1 {
private String username;
public PropertyDemo1(String username) {
this.username = username;
}
public void test1(){
System.out.println("demo.........."+username);
}
}
(2).xml配置
- constructor-arg
- name屬性值:類里面定義的屬性名稱
- value屬性值: 設置具體的值
<!--使用有參構造注入屬性-->
<bean id="demo1" class="IOC.PropertyDemo1">
<!-- 使用有參構造注入-->
<constructor-arg name="username" value="小王"></constructor-arg></bean>
(3)測試
import Bean.User;
import IOC.PropertyDemo1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by pc on 2017/9/7.
*/
public class TextIOC {
public static void main(String[] args) {
//加載Spring配置文件,并創建對象
ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
//得到配置的對象
PropertyDemo1 demo1 = (PropertyDemo1) context.getBean("demo1");
demo1.test1();
}
}
結果顯示
2 set方法注入
(1)測試setBook類
package IOC;
/**
* Created by pc on 2017/9/9.
*/
public class Book {
private String bookname;
public void setBookname(String bookname) {
this.bookname = bookname;
}
public void demobook(){
System.out.println("book.........."+bookname);
}
}
(2).xml配置
- property
- name屬性值:類里面定義的屬性名稱
- value屬性值: 設置具體的值
<!--使用set方法注入屬性-->
<bean id="book" class="IOC.Book">
<!-- 注入屬性值
name 屬性值:類里面定義的屬性名稱
value 屬性值: 設置具體的值
-->
<property name="bookname" value="天龍八部"></property>
</bean>
(3)測試代碼
import Bean.User;
import IOC.Book;
import IOC.PropertyDemo1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by pc on 2017/9/7.
*/
public class TextIOC {
public static void main(String[] args) {
//加載Spring配置文件,并創建對象
ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
//得到配置的對象
Book book = (Book) context.getBean("book");
book.demobook();
}
}
運行結果
三 注入對象類型屬性(重點)
1 UserDao
package IOC;
public class UserDao {
public void dao(){
System.out.println("dao.................");
}
}
2 UserService
package IOC;
public class UserService {
//1.定義dao類型屬性
private UserDao userDao;
//2.生成set方法
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void add(){
System.out.println("servie.................");
userDao.dao();
}
}
3 .xml
<bean id="userdao" class="IOC.UserDao"></bean>
<bean id="userService" class="IOC.UserService">
<!--注入dao對象
name屬性;service類里面屬性名稱
ref屬性(要注入哪個對象):dao配置bean標簽中id值(不要寫value屬性,因為剛才是字符串,現在是對象)
-->
<property name="userDao" ref="userdao"></property>
</bean>
注釋:
(1)先配置UserDAO和UserService兩個對象
(2)在要被注入的UserService配置中,配置注入UserDao
- name屬性;service類里面屬性名稱
- ref屬性(要注入哪個對象):dao配置bean標簽中id值(不要寫value屬性,因為剛才是字符串,現在是對象)
4 測試
import Bean.User;
import IOC.Book;
import IOC.PropertyDemo1;
import IOC.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TextIOC {
public static void main(String[] args) {
//加載Spring配置文件,并創建對象
ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
//得到配置的對象
UserService us = (UserService) context.getBean("userService");
us.add();
}
}
運行結果
結果顯示
四 P名稱空間注入
1 Person
package IOC;
public class Person {
private String pname;
public void setPname(String pname) {
this.pname = pname;
}
public void test(){
System.out.println("person........."+pname);
}
}
2 .xml配置
<!--p名稱空間注入-->
<bean id="person" class="IOC.Person" p:pname="Jack"></bean>
3 測試
import Bean.User;
import IOC.Book;
import IOC.Person;
import IOC.PropertyDemo1;
import IOC.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TextIOC {
public static void main(String[] args) {
//加載Spring配置文件,并創建對象
ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
//得到配置的對象
Person person= (Person) context.getBean("person");
person.test();
}
}
結果顯示
五 注入復雜類型
- 數組
- list集合
- map集合
- properties
1 Person
package IOC;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* Created by pc on 2017/9/9.
*/
public class Person {
private String pname;
private String arrs[];
private List<String> list;
private Map<String,String> map;
private Properties properties;
public void setArrs(String[] arrs) {
this.arrs = arrs;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void setPname(String pname) {
this.pname = pname;
}
public void test(){
System.out.println("person........."+pname);
System.out.println("arrs...."+arrs);
System.out.println("list...."+list);
System.out.println("map...."+map);
System.out.println("properties......"+properties);
}
}
2 .xml配置
<bean id="person" class="IOC.Person">
<!--數組-->
<property name="arrs">
<list>
<value>張三</value>
<value>李四</value>
<value>王麻子</value>
</list>
</property>
<!--list-->
<property name="list">
<list>
<value>張三list</value>
<value>李四list</value>
<value>王麻子list</value>
</list>
</property>
<!--map-->
<property name="map">
<map>
<entry key="1" value="張三map"></entry>
<entry key="2" value="李四map"></entry>
<entry key="3" value="王麻子map"></entry>
</map>
</property>
<!--properties-->
<property name="properties">
<props>
<prop key="driverclass">com.mysql.jdbc.Driver</prop>
<prop key="username">root</prop>
</props>
</property>
</bean>
3 測試
import Bean.User;
import IOC.Book;
import IOC.Person;
import IOC.PropertyDemo1;
import IOC.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TextIOC {
public static void main(String[] args) {
//加載Spring配置文件,并創建對象
ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
//得到配置的對象
Person person= (Person) context.getBean("person");
person.test();
}
}
結果顯示
六 IOC和DI區別
1 IOC:控制反轉,把對象創建交給spring進行配置
2 DI:依賴注入,向類里面的屬性中設置值
3 關系:依賴注入不能單獨存在,需要在IOC基礎之上完成操作