spring的依赖注入

spring的依赖注入的三种方式

  • 使用构造函数提供
  • 使用set方法提供
  • 使用注解提供

使用构造函数提供

使用标签:constructor-arg
使用位置:标签内部
使用例子:


    <bean id="addressService" class="service.impl.AddressService">
        <constructor-arg name="aa" value="12"/>
        <constructor-arg name="bb" value="bb"/>
        <constructor-arg name="date" ref="new"/>
    bean>
    
    <bean id="new" class="java.util.Date"/>

AddressService.java文件

public class AddressService implements IAddressService {
    private Integer aa;
    private String bb;
    private Date date;
    public AddressService(Integer aa, String bb, Date date) {
        this.aa = aa;
        this.bb = bb;
        this.date = date;
    }
    public void saveAddress() {
        System.out.println("AddressService{" +
                "aa=" + aa +
                ", bb='" + bb + '\'' +
                ", date=" + date +
                '}');
    }
}

使用set方法进行依赖注入

使用标签:property
使用位置:标签内部
使用例子:

	
       <bean id="new" class="java.util.Date" />
    <bean id="addressService" class="service.impl.AddressService">
        <property name="aa" value="12"/>
        <property name="bb" value="bb"/>
        <property name="date" ref="new"/>
        
        <property name="map">
            <map>
                <entry key="1" value="1111"/>
                <entry key="2" value="2222"/>
                <entry key="3" value="3333"/>
            map>
        property>

        <property name="set">
            <set>
                <value>1111value>
                <value>1111value>
                <value>1111value>
            set>
        property>
    bean>

AddressService.java文件

public class AddressService implements IAddressService {
    private Integer aa;
    private String bb;
    private Date date;
    private Map<String,Integer> map;
    public void setAa(Integer aa) { this.aa = aa;}
    public void setBb(String bb) {this.bb = bb;}
    public void setDate(Date date) {this.date = date;}
    public void setMap(Map<String, Integer> map) { this.map = map; }
    public void saveAddress() {
        System.out.println("AddressService{" +
                "aa=" + aa +
                ", bb='" + bb + '\'' +
                ", date=" + date +
                "map=" + map +
                '}');
    }
}

使用注解方式注入依赖

  • 使用注解:
@Component: 把普通的pojo实例化到spring容器中,相当于,当组件不容易分类是使用这个
@Controller: 一般用于表现层
@Service: 一般用于业务层
@Repository: 一般用于持久层
@Component  @Controller  @Service  @Repository这三个注解都有一个value属性,value值是指注入依赖的id名称,value属性可以不写,默认使用依赖名,依赖名首字母小写
这三个使三层对象更加清晰
@Autowired: 按照类型注入,只要容器中有唯一一个bean对象和要注入的变量类型匹配,就可以注入成功,
					  如果一个也没有则报错,
    		          如果有多个匹配,则使用@Autowired+@Qualifier(value = "容器中bean对象的id")结合的方式进行处理,
                     或者使用@Resource(name = "容器中bean对象的id")的方式进行处理
@Autowired  @Qualifier  @Resource ---->这三个注解都只能注入bean类型的数据,基本类型和string类型无法使用上述注解
@Value 注解可以对基本类型和string类型实现注入

在使用注解时,要告知spring创建容器时要使用的包,配置一个bean.xml文件


<beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:context="http://www.springframework.org/schema/context"
               xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
        
        <context:component-scan base-package="service.impl"/>
        <context:component-scan base-package="dao.impl"/>
        <context:component-scan base-package="controller"/>
beans>

使用例子:

@Repository(value = "addressDao")
public class AddressDaoImpl implements IAddressDao {
    public void saveAddress(){
        System.out.println("模拟打印1111111");
    }
}

@Service(value = "addressService")
public class AddressService implements IAddressService {
//    @Autowired
//    @Qualifier(value = "addressDao")
    @Resource(name = "addressDao2")
   private IAddressDao iAddressDao;
    public void saveAddress() {
        iAddressDao.saveAddress();
    }
}

@Controller
public class Cline {
    /**
     * 获取spring的ioc核心容器,并根据id获取对象
     * ApplicationContext的三个常用实现类
     *      ClassPathXmlApplicationContext:加载类路径下的配置文件,要求配置文件必须在类路径下,这种方式相对于第二个更常用。
     *      FileSystemXmlApplicationContext:可以加载磁盘任意路径下的配置文件
     *      AnnotationConfigApplicationContext:用于读取注解创建容器
     * @param args
     */
    public static void main(String[] args) {
//        获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean2.xml");
//          根据id获取bean对象
        IAddressService addressService = (IAddressService)ac.getBean("addressService");
        addressService.saveAddress();
    }
}

你可能感兴趣的:(spring,spring)