Spring:使用注解开发

bean

1. 使用注解开发需要导入spring的一系列包;

<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.9.RELEASE</version>
        </dependency>

2. 需要再配置文件中加一个约束:context;

 <?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/context
           http://www.springframework.org/schema/context/spring-context.xsd">
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

3. 配置扫描组件

<!--自动扫描指定包下的注解-->
<context:component-scan base-package="com.kuang.demo"/>

4. 编写代码:

//等价于:<bean id="user" class="com.kuang.demo.User"/>

/*
@Component : 组件 bean
@Controller :web层
@Service : service层
@Repository :dao层
 */

@Component("user")
public class User {
    public String name = "秦疆1号";
}

IOC注入

  1. 可以不用提供set方法,可以直接在属性名上添加一个@Values(值);
@Controller("user2")
public class User2 {
    //    <bean class="com.kuang.demo.User2" id="user2">
    //        <property name="name" value="秦疆2号"/>
    //    </bean>

    @Value("秦疆")
    private String name;

    public String getName() {
        return name;
    }
}
  1. 加入有set呢,直接在set方法上面加上@Values(值);
@Controller("user2")
public class User2 {
    //    <bean class="com.kuang.demo.User2" id="user2">
    //        <property name="name" value="秦疆2号"/>
    //    </bean>


    private String name;

    public String getName() {
        return name;
    }
    
    @Value("秦疆")
    public void setName(String name) {
        this.name = name;
    }
}

自动装配【了解】

@Component("user3")
public class User3 {

    @Value("秦疆3号")
    public String name;
/*
    @Autowired //自动注入按照类型匹配,如果存在两个自动匹配对象的值,则无法匹配;
    @Qualifier("dog1")
    public Dog dog;
*/


    //不是spring的包,是annotation的包;
    @Resource(name = "dog1")
    public Dog dog;
}

作用域【了解】

@Scope("prototype") //Spring默认是单例模式的,如果要改成多例模式,加上作用域prototype即可
@Scope("prototype")
@Component("user3")
public class User3 {

}

AOP

回看AOP注解实现

注解和XML对比

  • xml可以适用于任何场景,结构清晰,推荐使用。
  • 注解不是自己提供的类,存在局限性;好处:开发简单,方便

注解xml可以整合开发

xml和注解的最佳实践:

  • xml管理bean
  • 注解完成属性的注入
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值