IOC的注解方式

1、创建web项目,引入jar包

      

2、引入Spring配置文件

<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 definitions here -->
	
</beans>

3、创建接口和实现类

4、开启Spring组建扫描

    <!-- 使用IOC注解开发,配置组件扫描(哪些包下的类使用IOC的注解) -->
	<context:component-scan base-package="com.zzu.spring"></context:component-scan>
	<!-- 在没有扫描的情况下,使用属性注入的注解: -->
	<context:annotation-config></context:annotation-config>

5、Spring IOC注解的详解

      一、@Component:组件

                修饰一个类,将这个类交给Spring管理。有三个衍生注解(@Controller:web层;@Service:业务层;@Reposi:                      Dao层)

@Component("userDao")//相当于<bean id="userDao" class="com.zzu.spring.demo1.UserDao">
public class UserDao implements IUserDao {
	
	@Override
	public void save() {
		System.out.println("11111111" );
	}
}

     二、属性注入的注解

               普通属性:@value 设置普通属性的值

               对象类型属性: @autowired 设置对象类型的属性的值,但是按照类型完成属性注入

                                         @resource(name=" ") 按照名称完成属性注入

     三、Bean的其他注解

                 生命周期相关的注解

                 @PostConstruct :初始化方法

                 @PreDestroy :     销毁方法

                 作用范围相关的注解

                 @Scope:作用范围,默认单例(多例     @Scope("prototype"))

              

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值