spring 常用的注入方式有哪些

一、xml中配置

bean 的申明、注册
<bean> 节点注册 bean
<bean> 节点的 factory-bean 参数指工厂 bean,factory-method 参数指定工厂方法

 

bean 的注入
<property> 节点使用 set 方式注入
<constructor-arg> 节点使用 构造方法注入

 

实测代码

maven pom 文件 

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.2.4.RELEASE</version>
</dependency>
 
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.2.4.RELEASE</version>
</dependency>


1、setter注入:<bean> + <property>,set方法注入

class Bowl

package constxiong.interview.inject;
 
public class Bowl {
 
    public void putRice() {
        System.out.println("盛饭...");
    }
 
}
class Person

package constxiong.interview.inject;
 
public class Person {
 
    private Bowl bowl;
    
    public void eat() {
        bowl.putRice();
        System.out.println("开始吃饭...");
    }
 
    public void setBowl(Bowl bowl) {
        this.bowl = bowl;
    }
    
}


spring 配置文件

<?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">
        
    <bean id="bowl" class="constxiong.interview.inject.Bowl" />
    
    <bean id="person" class="constxiong.interview.inject.Person">
        <property name="bowl" ref="bowl"></property>
    </bean>
    
</beans>


测试类

package constxiong.interview.inject;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class InjectTest {
 
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring_inject.xml");
        Person person = (Person)context.getBean("person");
        person.eat();
    }
}

2、构造方法注入:修改为 配置文件和class Person,<bean> + <constructor-arg> 节点使用

class Person

package constxiong.interview.inject;
 
public class Person {
 
    private Bowl bowl;
    
    public Person(Bowl bowl) {   // 构造函数,若需要注册多个类构造函数需要有多个类参数的构造函数
        this.bowl = bowl;
    }
    
    public void eat() {
        bowl.putRice();
        System.out.println("开始吃饭...");
    }
    
}


spring 配置文件

<?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">
        
    <bean id="bowl" class="constxiong.interview.inject.Bowl" />
    
    <bean id="person" class="constxiong.interview.inject.Person">
        // 若需要注册多个类, 需要添加对应对应的类引用
        <constructor-arg name="bowl" ref="bowl"></constructor-arg>
    </bean>
    
</beans>

 

3、静态工厂方法:<bean> 节点 factory-method 参数指定静态工厂方法

工厂类,静态工厂方法

package constxiong.interview.inject;
 
public class BowlFactory {
 
    public static final Bowl getBowl() {
        return new Bowl();
    }
    
}


spring 配置文件

<?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">
        
    <bean id="bowl" class="constxiong.interview.inject.BowlFactory" factory-method="getBowl"/>
    
    <bean id="person" class="constxiong.interview.inject.Person">
        <constructor-arg name="bowl" ref="bowl"></constructor-arg>
    </bean>
    
</beans>


4、非静态工厂方法,需要指定工厂 bean 和工厂方法

工厂类,非静态工厂方法

package constxiong.interview.inject;
 
public class BowlFactory {
 
    public Bowl getBowl() {
        return new Bowl();
    }
    
}


配置文件

<?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">
    
    <bean id="bowlFactory" class="constxiong.interview.inject.BowlFactory"></bean>   
    <bean id="bowl" factory-bean="bowlFactory" factory-method="getBowl"/>
    
    <bean id="person" class="constxiong.interview.inject.Person">
        <constructor-arg name="bowl" ref="bowl"></constructor-arg>
    </bean>
    
</beans>


2、注解

bean 的申明、注册
@Component //注册所有bean
@Controller //注册控制层的bean
@Service //注册服务层的bean
@Repository //注册dao层的bean

 

bean 的注入
@Autowired 作用于 构造方法、字段、方法,常用于成员变量字段之上。
@Autowired + @Qualifier 注入,指定 bean 的名称
@Resource JDK 自带注解注入,可以指定 bean 的名称和类型等

 

测试代码

1、spring 配置文件,设置注解扫描目录

<?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="constxiong.interview" />
    
</beans>


class Bowl

package constxiong.interview.inject;
 
import org.springframework.stereotype.Component;
//import org.springframework.stereotype.Controller;
//import org.springframework.stereotype.Repository;
//import org.springframework.stereotype.Service;
 
@Component //注册所有bean
//@Controller //注册控制层的bean
//@Service //注册服务层的bean
//@Repository //注册dao层的bean
public class Bowl {
 
    public void putRice() {
        System.out.println("盛饭...");
    }
 
}


class Person

package constxiong.interview.inject;
 
//import javax.annotation.Resource;
//
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
 
@Component //注册所有bean
//@Controller //注册控制层的bean
//@Service //注册服务层的bean
//@Repository //注册dao层的bean
public class Person {
 
    @Autowired
//    @Qualifier("bowl")
//    @Resource(name="bowl")
    private Bowl bowl;
 
    public void eat() {
        bowl.putRice();
        System.out.println("开始吃饭...");
    }
    
}


测试类同上

 

A、B、C、D、E 测试结果都ok

盛饭...
开始吃饭...
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值