Spring中Ioc容器的注入方式

时间:2023-02-10 19:42:15

1 通过setter方法注入

bean类:

package com.test;   

public class UserServiceImplement implements IUserService   

{
private IUserDao user; public IUserDao getUser() {
return user;
} public void setUser(IUserDao user) {
this.user = user;
} public void saveUser() {
user.addUser();
}
}

Xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="userdao" class="com.test.UserDaoImplement"/>
<bean id="userservice" class="com.test.UserServiceImplement">
<property name="user" ref="userdao" />
</bean>
</beans>

注:setter方式注入,对应的注入依靠属性,必须要有setter方法。

2 通过构造方法注入

bean类:

package com.test;   

public class UserServiceImplement implements IUserService {
private IUserDao user;
int age; public UserServiceImplement(IUserDao user, int age) {
this.user = user;
this.age = age;
} public void saveUser() {
user.addUser();
System.out.println(this.age);
}
}

xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="userdao" class="com.test.UserDaoImplement" />
<bean id="userservice" class="com.test.UserServiceImplement">
<constructor-arg index="0" type="com.test.IUserDao" ref="userdao"></constructor-arg>
<constructor-arg index="1" value="24"></constructor-arg>
</bean>
</beans>

注:

    • 对于<construcotr-arg>标签中的index属性,假如构造方法只有一个参数的时候可以不指定;它的下标从0开始,表示构造方法参数的索引;假如构造方法有多个参数必须指定索引。除此之外还有一个type属性,type是一个可选属性,这个属性用来指定被注入的参数的参数类型,一定要跟构造方法中的参数类型一致,假如是一个接口,那么也不应传它的实现类。
    • 假如构造函数的的参数类型是基本数据类型,那么就不用ref属性了,而用value属性设置它的值,而且这些数据类型会自动打包和解包;
    • 要注意bean的作用域问题。
    • 被注入的bean类,一定要有构造方法。

    3 通过注解进行注入

    1. @Resource
    2. @Autowired

    二者区别:

      • @Resource标注是由JDK提供的,而@Autowired标注是由Spring提供的,因而@Autowired标注会与Spring紧密耦合,所以推荐使用@Resource标注
      • @Resource默认是按照名称来装配注入的,当找不到与名称匹配的bean才会按照类型来装配注入
      • @Autowired默认是按照类型装配注入的,假如想按照名称来转配注入,则需要结合@Qualifier一起使用
      • @Resource和@Autowired都可以用来标注字段或者setter方法

    @Resource注入方式代码:

    package com.test;   
    
    import javax.annotation.Resource;   
    
    public class UserServiceImplement implements IUserService {   
    
        @Resource
    private IUserDao user; private IUserDao user1; public IUserDao getUser1() {
    return user1;
    } @Resource
    public void setUser1(IUserDao user1) {
    this.user1 = user1;
    } public void saveUser() {
    user.addUser();
    System.out.println("user注入成功");
    user1.addUser();
    System.out.println("user1注入成功");
    }
    }

    xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <context:annotation-config />
    <bean id="user" class="com.test.UserDaoImplement"/>
    <bean id="user1" class="com.test.UserDaoImplement"/>
    <bean id="userservice" class="com.test.UserServiceImplement"/>
    </beans>

    @Autowired注入方式代码:

    package com.test;   
    
    import org.springframework.beans.factory.annotation.Autowired;   
    
    public class UserServiceImplement implements IUserService {   
    
        @Autowired
    private IUserDao user; public IUserDao getUser() {
    return user;
    } public void setUser(IUserDao user) {
    this.user = user;
    } public void saveUser() {
    user.addUser();
    System.out.println("user注入成功");
    }
    }

    xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <context:annotation-config />
    <bean id="user" class="com.test.UserDaoImplement"/>
    <bean id="userservice" class="com.test.UserServiceImplement"/>
    </beans>