springIOC基于注解开发

三.IOC基于注解开发
1.导包
核心包:spring-beans-4.2.4.RELEASE.jar,spring-context-4.2.4.RELEASE.jar,spring-core-4.2.4.RELEASE.jar,spring-expression-4.2.4.RELEASE.jar
日志包:com.springsource.org.apache.commons.logging-1.1.1.jar 日志规范
com.springsource.org.apache.log4j-1.2.15.jar 日志实现
aop包:spring-aop-4.2.4.RELEASE.jar
同时日志还需要log4j.properties 配置文件

2.配置
需要在applicationContext.xml文件中开启注解扫描,让spring去扫描指定位置下的类。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 告知spring在创建容器时要扫描的包。当配置了此标签之后,spring创建容器就会去指定的包及其子包下找对应的注解
        标签是在一个context的名称空间里,所以必须在约束中导入context名称空间
    -->
    <context:component-scan base-package="com.itheima"></context:component-scan>
</beans>

3.注解分类

1.用于创建bean对象
@Component
作用:就相当于配置了一个bean标签,交给spring来管理
它能出现的位置:类上面
属性:value。指定bean的id,当不写时,它有默认值,默认值是:当前类的短名首字母小写(例如类名ICustomerServiceImpl,那么bean名iCustomerServiceImpl)
由此注解衍生的三个注解和@Component作用一样:
@Controller 一般用于表现层(web层)的注解
@Service 一般用于业务层(service层)的注解
@Repository 一般用于持久层(dao层)的注解
2.用于注入数据的
当我们使用注解注入时,类中可以去掉set方法
@Autowired
作用:自动按照类型注入(spring会在容器中寻找满足被注入对象类型的bean进行注入),当spring容器中这个类型的bean多于一个时,则报错
@Qualifier
作用:在自动按照类型注入的基础之上,再按照bean的id注入。它给类成员注入数据时,不能独立使用(要配合@Autowired)。但是再给方法的形参注入数据时,可以独立使用
属性:
value:用于指定bean的id
@Resource(name = “customerDao”)
作用:直接按照bean的id注入
@Value
作用:用于注入基本类型和String类型。它可以借助spring的el表达式读取properties文件中的配置
3.用于改变作用范围的
@Scope(“prototype”)
作用:用于改变bean的作用范围
属性:
value:用于指定范围的取值
取值和xml中scope属性的取值是一样的。singleton prototype request session globalsession
4.和生命周期相关的

	@PostConstruct	相当于 init-method
	@PreDestroy		相当于 destroy-method

eg

@Controller("userAction")
public class UserAction {

    @Resource(name = "userService")
    private UserService userService;

    @PostConstruct
    public void init(){
        System.out.println("初始化方法");
    }

    public void save(){
        System.out.println("action save方法");
        userService.add();
    }

    @PreDestroy
    public void destory(){
        System.out.println("销毁方法");
    }
}


eg
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 告知spring在创建容器时要扫描的包。当配置了此标签之后,spring创建容器就会去指定的包及其子包下找对应的注解
        标签是在一个context的名称空间里,所以必须在约束中导入context名称空间
    -->
    <context:component-scan base-package="com.itheima"></context:component-scan>
</beans>

// 指定类交给spring来创建,指定bean的id为customerService
@Service("customerService")
@Scope("prototype")
public class ICustomerServiceImpl implements ICustomerService {
//    @Autowired
//    @Qualifier("customerDao")
    @Resource(name = "customerDao")
    private ICustomerDao customerDao;

    @Value("miralce")
    private String name;

    @Override
    public void saveCustomer() {
        System.out.println("业务层调用持久层" + name);
        customerDao.saveCustomer();
    }
}

@Repository("customerDao")
public class ICustomerDaoImpl implements ICustomerDao {

    @Override
    public void saveCustomer() {
        System.out.println("持久层保存了客户");
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值