springIOC注入的两种方式

spring编码风格

schemal-based-------xml

annotation-based-----annotation

java-based----java Configuration

 

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lixiang</groupId>
    <artifactId>spring-ioc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

</project>

1、set方法注入

//spring.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dao" class="com.lixiang.DaoImpl"></bean>


    <bean id="service" class="com.lixiang.SpringImpl">
        <property name="dao" ref="dao"/>
    </bean>

</beans>


/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
public class SpringImpl {
    private Dao dao;

    // spring的set方法注入

    public void setDao(Dao dao) {
        this.dao = dao;
    }

    public void test(){
        dao.test();
    }
}

/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
public interface Dao {
    void test();
}

/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
public class DaoImpl implements Dao {
    @Override
    public void test() {
        System.out.println("Dao");
    }
}

/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
public class TestDemo {
    @Test
    public void fun1(){
        ApplicationContext ca = new ClassPathXmlApplicationContext("classpath:spring.xml");
        SpringImpl springImpl = (SpringImpl) ca.getBean("service");
        springImpl.test();
    }
}

 

2、构造方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="dao" class="com.lixiang.DaoImpl"></bean>


    <bean id="service" class="com.lixiang.SpringImpl">
        <constructor-arg name="dao" ref="dao"/>
    </bean>

</beans>

/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
public class SpringImpl {
    private Dao dao;

    // 构造方法
    public SpringImpl(Dao dao){
        this.dao = dao;
    }

    public void test(){
        dao.test();
    }
}

/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
public class TestDemo {
    @Test
    public void fun1(){
        ApplicationContext ca = new ClassPathXmlApplicationContext("classpath:spring.xml");
        SpringImpl springImpl = (SpringImpl) ca.getBean("service");
        springImpl.test();
    }
}

以上,需要自己在spring.xml里描述类的依赖关系,spring可以开启自动依赖


可以在spring.xml文件里开启自动依赖,此时不需要在<bean>标签里写<property name="dao" ref="dao"/>和<constructor-arg name="dao" ref="dao"/>此时只需要注入<bean>即可,依赖关系恶意省略.自动注入方式有byType,byName,no和constructor

Table 2. Autowiring modes
ModeExplanation

no

(Default) No autowiring. Bean references must be defined by ref elements. Changing the default setting is not recommended for larger deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents the structure of a system.

byName

Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name and it contains a master property (that is, it has a setMaster(..) method), Spring looks for a bean definition named master and uses it to set the property.

byType

Lets a property be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byTypeautowiring for that bean. If there are no matching beans, nothing happens (the property is not set).

constructor

Analogous to byType but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
        default-autowire="byName">

    <bean id="dao" class="com.lixiang.DaoImpl"/>
    <bean id="dao1" class="com.lixiang.DaoImpl1"/>

    <bean id="service" class="com.lixiang.SpringImpl"/>

</beans>

基于注解注入

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd"
        >
    <!--开启注解扫描-->
    <context:component-scan base-package="com"/>

</beans>

/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
@Component("service")
public class SpringImpl {
    @Autowired
    private Dao dao1;

    public void test(){
        dao1.test();
    }
}

/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
@Component("dao1")
public class DaoImpl1 implements Dao {
    @Override
    public void test() {
        System.out.println("Dao1");
    }
}


/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
public class TestDemo {
    @Test
    public void fun1(){
        ApplicationContext ca = new ClassPathXmlApplicationContext("classpath:spring.xml");
        SpringImpl springImpl = (SpringImpl) ca.getBean("service");
        springImpl.test();
    }
}

Java配置类注入

/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
@Configuration
@ComponentScan("com")
public class SpringConfig {
}

/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
public class TestDemo {
    @Test
    public void fun1(){
        ApplicationContext ca = new AnnotationConfigApplicationContext(SpringConfig.class);
        SpringImpl springImpl = (SpringImpl) ca.getBean("service");
        springImpl.test();
    }
}

/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
@Component("service")
public class SpringImpl {
    @Autowired
    private Dao dao;

    public void test() {
        dao.test();
    }
}

/**
 * description
 *
 * @version 1.0
 * @date 2019/8/14
 */
@Component
public class DaoImpl1 implements Dao {
    @Override
    public void test() {
        System.out.println("Dao1");
    }
}

总结:

@Autowired和@Resource的区别

当@Autowired根据byType自动依赖查找不到bean时,会根据byName的方式,去寻找跟属性名一致的bean,此时一个接口可以有多个实现类,但是每个实现类的名称要不一样,如果依赖的属性名跟实现类的名称不一致则会报错

@Resource则反之

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值