Spring中常见Bean的注册&依赖注入

Spring常见Bean的注册&依赖注入

一、Bean的注册

public class Department {
    private Long id;
    private String name;
    
    /*提供无参构造,有参构造,setter和getter方法,覆写toString方法*/
}
public class Employee {
    private Long id;
    private String username;
    private String sex;
    private Department department;

    /*提供无参构造,有参构造,setter和getter方法,覆写toString方法*/
}

1、在xml中注册

1.1、直接配置Bean
<?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="employee" class="该类Employee的全限定名(包名+类)"/>
    
    <!--调用有参构造方法。每一个参数都必须手动设置。-->
    <bean id="employee2" class="该类Employee的全限定名">
        <!--通过构造器中参数的索引注入。value=""普通类型赋值-->
        <constructor-arg index="0" value="1"/>
        <!--通过构造器中参数的名字注入-->
        <constructor-arg name="username" value="张三"/>
        <!--通过构造器中参数的类型注入,性别-->
        <constructor-arg type="java.lang.String" value=""/>
        <!--引入对象,外部引入。ref=""指向外部bean的id-->
        <constructor-arg type="该类Department的全限定名" ref="department"/>
<!--    引入对象,内部引入。内部bean不能从容器中直接获取。    
        <constructor-arg name="department" >
            <bean id="department" class="该类Department的全限定名">
                <constructor-arg name="id" value="1"/>
                <constructor-arg name="name" value="人事部"/>
            </bean>
        </constructor-arg>-->
    </bean>
    
    <bean id="department" class="该类Department的全限定名"/>
    
    <!--调用setter方法。如果某个参数不设置值,则为默认值。-->
    <bean id="employee3" class="该类Employee的全限定名">
        <property name="id" value="1"/>
        <property name="username" value="李四"/>
        <property name="sex" value=""/>
        <property name="department " >
            <bean id="department3" class="该类Department的全限定名">
                <property name="id" value="2"/>
                <property name="name" value="开发部"/>
            </bean>
        </property>
    </bean>
    
</beans>
1.2、静态工厂
/*静态工厂*/
public class EmployeeFactory {
    /*获取实例的静态方法*/
    public static Employee getEmployee(){
        return new Employee();
    }
}
<?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">
    
    <!--静态工厂,提供静态工厂的全限定名,和静态工厂中获取对象的方法。由于是静态方法,所以不需要实例化静态工厂。此时只实例化Employee,而不实例化静态工厂。-->
    <bean id="employee4" class="静态工厂全限定名" factory-method="getEmployee"/>
    
</beans>
1.3、实例简单工厂
/*实例简单工厂,其中的方法不是静态方法,在xml中既要实例化工厂,也会根据工厂去实例化Employee,
 * 因为不是静态方法,需要对象调用,所以要实例化工厂*/
public class EmployeeFactory2 {
    public Employee getEmployee(){
        return new Employee();
    }
}
<?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="employeeFactory2" class="实例简单工厂EmployeeFactory2的全限定名"/>
    <!--实例化Employee-->
    <bean id="employee5" factory-bean="employeeFactory2" factory-method="getEmployee"/></beans>
1.4、工厂类实现接口FactoryBean,覆写方法
/*工厂实现接口FactoryBean<T>,覆写三个方法*/
public class EmployeeFactory3 implements FactoryBean<Employee> {
    @Override
    public Employee getObject() throws Exception {//获取对象
        return new Employee();
    }

    @Override
    public Class<?> getObjectType() {//对象类型
        return Employee.class;
    }

    @Override
    public boolean isSingleton() {//是否单例
        return true;
    }
}
<?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">   
   
    <!--写一个工厂类实现FactoryBean接口,覆写三个方法。这里没有写工厂方法,因为底层有默认-->
    <bean id="employee6" class="该类EmployeeFactory3的全限定名"/>
</beans>

2、通过注解注册

2.1、@Configuration和@Bean结合
@Configuration//表示该类是一个配置类
public class KeyConfiguration {

    @Bean//注册Bean
    public Key getKey(){
        return new Key();
    }
}

就相当于xml中的

<bean id="key" class="该类Key的全限定名"/>
public class Key {
    private Long id;
    private String name;

    /*提供无参构造,有参构造,setter和getter方法,覆写toString方法*/
}
2.2、自动扫描Spring提供的注解
2.2.1、扫描包路径
@ComponentScan("包路径")
@Configuration
public class MyConfig {
    
}

就相当于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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 组件搜索,扫描包路径 -->
    <context:component-scan base-package="包路径" />
</beans>
2.2.2、给需要注册的类打上注解

@Component,或者Controller层@Controller,Service层@Service,Dao层@Repository

二、依赖注入

1、@Autowired和@Qualifier

@Autowired默认byType注入,单例模式,只有唯一一个Bean的时候,直接在字段上打上@Autowired注解即可。多例模式,有多个Bean时,需要和@Qualifier(“对象名”)结合使用。

	@Autowired
    @Qualifier("employee2")
    private Employee employee2;

2、@Resource

@Resource是byName注入,当字段名和要注入的Bean名字相同时,直接打上@Resource注解即可。

当字段名和要注入的Bean名字不同的时候,使用@Resource(name=“对象名”)。

    @Resource(name="employee2")
    private Employee employee;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值