一 屬性注入介紹
1 創(chuàng)建對(duì)象時(shí)候浸策,向類里面屬性設(shè)置值。
2 Java設(shè)置屬性的三種方法
(1) 使用set方法注入
public class User {
private String name;
public void setName(String name){
this.name = name;
}
}
User user = new User();
user.setName("abcd");
(2) 有參構(gòu)造注入
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方法注入(重點(diǎn))
(2)有參構(gòu)造注入
二 Spring框架屬性注入
1 有參構(gòu)造注入
(1) 有參構(gòu)造
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屬性值: 設(shè)置具體的值
<!--使用有參構(gòu)造注入屬性-->
<bean id="demo1" class="IOC.PropertyDemo1">
<!-- 使用有參構(gòu)造注入-->
<constructor-arg name="username" value="小王"></constructor-arg></bean>
(3)測(cè)試
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配置文件,并創(chuàng)建對(duì)象
ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
//得到配置的對(duì)象
PropertyDemo1 demo1 = (PropertyDemo1) context.getBean("demo1");
demo1.test1();
}
}
2 set方法注入
(1)測(cè)試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屬性值: 設(shè)置具體的值
<!--使用set方法注入屬性-->
<bean id="book" class="IOC.Book">
<!-- 注入屬性值
name 屬性值:類里面定義的屬性名稱
value 屬性值: 設(shè)置具體的值
-->
<property name="bookname" value="天龍八部"></property>
</bean>
(3)測(cè)試代碼
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配置文件惫确,并創(chuàng)建對(duì)象
ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
//得到配置的對(duì)象
Book book = (Book) context.getBean("book");
book.demobook();
}
}
三 注入對(duì)象類型屬性(重點(diǎn))
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對(duì)象
name屬性;service類里面屬性名稱
ref屬性(要注入哪個(gè)對(duì)象):dao配置bean標(biāo)簽中id值(不要寫value屬性手报,因?yàn)閯偛攀亲址F(xiàn)在是對(duì)象)
-->
<property name="userDao" ref="userdao"></property>
</bean>
注釋:
(1)先配置UserDAO和UserService兩個(gè)對(duì)象
(2)在要被注入的UserService配置中改化,配置注入U(xiǎn)serDao
- name屬性;service類里面屬性名稱
- ref屬性(要注入哪個(gè)對(duì)象):dao配置bean標(biāo)簽中id值(不要寫value屬性掩蛤,因?yàn)閯偛攀亲址F(xiàn)在是對(duì)象)
4 測(cè)試
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配置文件陈肛,并創(chuàng)建對(duì)象
ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
//得到配置的對(duì)象
UserService us = (UserService) context.getBean("userService");
us.add();
}
}
運(yùn)行結(jié)果
四 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 測(cè)試
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配置文件揍鸟,并創(chuàng)建對(duì)象
ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
//得到配置的對(duì)象
Person person= (Person) context.getBean("person");
person.test();
}
}
五 注入復(fù)雜類型
- 數(shù)組
- 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">
<!--數(shù)組-->
<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 測(cè)試
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配置文件,并創(chuàng)建對(duì)象
ApplicationContext context = new ClassPathXmlApplicationContext("Spring/applicationContext.xml");
//得到配置的對(duì)象
Person person= (Person) context.getBean("person");
person.test();
}
}
六 IOC和DI區(qū)別
1 IOC:控制反轉(zhuǎn)句旱,把對(duì)象創(chuàng)建交給spring進(jìn)行配置
2 DI:依賴注入阳藻,向類里面的屬性中設(shè)置值
3 關(guān)系:依賴注入不能單獨(dú)存在,需要在IOC基礎(chǔ)之上完成操作