Spring的零配置(Annotation)

    如今,几乎所有的主流Java框架都打算支持”零配置“,包括Struts2、Hibernate,以及现在要说的Spring,都开始支持使用Annotation来代替XML配置文件了。配置如下:

1. 标注Spring的Bean类和配置依赖

1.1 PersonService.java

package test;
public interface PersonService {
    public void testPerson();
}

1.2 PersonServiceImpl.java

package test;

import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

// 指定该Bean实例的作用域
@Scope("prototype")
// 标注表明这些Java类都将作为Spring的Bean类。 也可在Spring配置文件中指定Bean类
// 1.Component标注一个普通的Spring Bean类,可使用Component("personServiceImpl")指定实例名称
// 2.Controller标注一个控制器组件类
// 3.Service标注一个业务逻辑组件类
// 4.Repository标注一个DAO组件类
@Component
public class PersonServiceImpl implements PersonService{
    // 方法一、使用Resource配置依赖,可省略name属性,甚至边后面的setter方法都可以不要
    //@Resource(name="personDaoImpl")
    private PersonDao personDao;
    // 方法二、使用Resource配置依赖,设置注入所需的setter方法
    @Resource(name="personDaoImpl")
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
    public void testPerson() {
        System.out.println("注入:" + personDao.testPerson());
    }
}

1.3 PersonDao.java

package test;
public interface PersonDao {
    public String testPerson();
}
1.4 PersonDaoImpl.java

package test;

import org.springframework.stereotype.Component;

@Component
public class PersonDaoImpl implements PersonDao {
    public String testPerson() {
        return "依赖注入就是控制反转";
    }
}

2. bean.xml中配置Bean类的搜索路径

<?xml version="1.0" encoding="GBK"?>
<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">
    <!-- 自动扫描指定包及其子包下的所有Bean类 -->
    <context:component-scan base-package="test"/>
    
    <!-- 只包含以ServiceImpl、DaoImpl结尾的类,满足此条件时就算没有注解也会被当成Bean处理
    <context:component-scan
        base-package="org.crazyit.app.service">
        <context:include-filter type="regex"
            expression=".*ServiceImpl"/>
        <context:include-filter type="regex"
            expression=".*DaoImpl"/>
    </context:component-scan>
     -->
</beans>

3. 测试

package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
    public static void main(String[] args) {
        // 创建Spring容器,一旦获得了该容器,就可通过该容器访问Spring容器中的Bean
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        // 输出Spring容器中所有的Bean实例名
        System.out.println(java.util.Arrays.toString(ctx.getBeanDefinitionNames()));
        
        PersonServiceImpl p = ctx.getBean("personServiceImpl" , PersonServiceImpl.class);
        p.testPerson();    
    }
}

4. 其它

4.1 使用@PostConstruct 注解依赖关系注入完成后回调该方法

4.2 使用@PreDestroy 注解依赖关系销毁之前回调该方法

4.3 使用@DependsOn 强制初始化其它Bean

4.4 使用@Lazy 指定该Bean是否取消预初始化

4.5 使用@Autowired 指定自动装配该类型的Bean实例

4.6 使用@Qualifier("name") 允许根据Bean标识来精确装配


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值