Spring 依赖注入的两种方式
1、使用XML注入
1.1使用setter()方法
在注入属性的时候框架会根据方法名字找到你要注入的属性名从而完成注入。在配置文件中如果按setter()方法注入属性的话,属性名要和应用类中的名称保持一致,不然Spring容器会找不见你当前所要注入的属性完成属性值的注入。
1、在 applicationContext.xml 中引入约束
2、创建应用类
注意:在创建应用类的时候如果使用setter(0方法注入要提供一个无参构造器,如果不写构造器,java会默认的提供一个无参构造器,如果你在类中写了构造器,java将不再为你创建无参构造器。
package com.ma.spring.demo01;
public class User {
private Integer age;
private String name;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
3、在配置文件中注入属性
4、测试类
package com.ma.spring.demo01;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.beans.IntrospectionException;
import java.lang.reflect.Method;
public class UserTest {
@Test
public void test01() {
//1、创建Spring的工厂对象(BeanFactory ApplicationContext)
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//2、使用applicationContext.xml内部配置的bean来创建对象
User user= (User) applicationContext.getBean("user");
System.out.println(user);
//applicationContext.close();
}
}
5、效果
1.2使用构造器注入属性
1、在 applicationContext.xml 中引入约束
2、创建应用类(在类中要创建有参构造方法同时要有无参构造(如果按无参构造的方式创建Bean))
package com.ma.spring.demo01;
public class User {
private Integer age;
private String name;
public User() {
}
public User(Integer age, String name) {
this.age = age;
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
3、在配置文件中注入属性
4、测试类
package com.ma.spring.demo01;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.beans.IntrospectionException;
import java.lang.reflect.Method;
public class UserTest {
@Test
public void test01() {
//1、创建Spring的工厂对象(BeanFactory ApplicationContext)
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//2、使用applicationContext.xml内部配置的bean来创建对象
User user= (User) applicationContext.getBean("user");
System.out.println(user);
//applicationContext.close();
}
}
5、效果
2、使用注解注入
2.1按类型注入
@Autowired
UserService.class
package com.ma.spring.demo02;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public void addUser(){
System.out.println("UserService......addUser()...");
userDao.insert();
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
UserDao.class
package com.ma.spring.demo02;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
public void insert(){
System.out.println("UserDao.....insert()...");
}
}
测试类
package com.ma.spring.demo02;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceTest {
@Test
public void testAddUser(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
userService.addUser();
}
}
结果
2.2按名称注入
UserService.class
package com.ma.spring.demo02;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserService {
@Autowired
@Qualifier("userDao")
private UserDao userDao;
public void addUser(){
System.out.println("UserService......addUser()...");
userDao.insert();
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}
UserDao.class
package com.ma.spring.demo02;
import org.springframework.stereotype.Repository;
@Repository("userDao")
public class UserDao {
public void insert(){
System.out.println("UserDao.....insert()...");
}
}
测试类
package com.ma.spring.demo02;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceTest {
@Test
public void testAddUser(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
userService.addUser();
}
}
结果
3.XML与注解的选择
在注解还没有出现的时候,只能使用XML的方式
现在主流开发使用XML+注解混合使用
XML:用来管理 Bean
注解用来依赖注入
未来的趋势是纯注解,简化开发,但有一定的耦合。