Spring注解开发

Spring注解

1.使用注解须知:

1.导入约束:

xmlns:context=“http://www.springframework.org/schema/context”

2.×××配置注解支持:
context:annotation-config/

最终:

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

2.注解实现自动装配

使用Autowired:可以指定单个不重复的id,而如果要指定某个id时要使用@Qualifier(value = “xxx”)

public class People {
    /*zhushi自动导入*/
    @Autowired
    private Cat cat;
    @Qualifier(value = "dog2")
    private Dog dog;
    private String name;
 <bean id="dog1" class="com.zz.pojo.Dog"/>
    <bean id="dog2" class="com.zz.pojo.Dog"/>
    <!--使用@Qualifier(value = "dog2")进行指定装配-->
    <bean id="dog3" class="com.zz.pojo.Dog"/>
    <bean id="cat" class="com.zz.pojo.Cat"/>
    <bean id="cat2" class="com.zz.pojo.Cat"/>
    <bean id="people" class="com.zz.pojo.People">
         <property name="name" value="李四"/>

    </bean>

3.使用注解开发

在spring4后要保证aop包存在

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-irncuFDy-1584778596018)(C:\Users\19353\AppData\Roaming\Typora\typora-user-images\image-20200321115516293.png)]

使用注解要加入注解支持

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

1.bean

1.1 Component:组件

//等价于<bean id ="user" class="com.zz.pojo.User"/>
@Component
public class User {
    public String name="张三";
}

在使用Component注解时要指定扫描的包

<!--指定要扫描的包,这个包下的注解就会生效--> <context:component-scan base-package="com.zz.pojo"/> <context:annotation-config/>

1.2@Value:相当于<property name=“name” value=“张三”

@Component
public class User {
    //相当于<property name="name" value="张三"
    @Value("张三")
    public String name;
}

1.3衍生的注解

@Component有几个衍生的注解,我们在web开发中,分三层架构

  • dao: @Repository
  • service : @Service
  • controller : @Controller

这四个注解作用相同,都是将某个类注册到Spring中,装配Bean

2.Scope:指定模式

@Scope("singleton")
  <!--单例模式-->
    <bean scope="singleton"/>
    <!--原型模式-->
  <!--  <bean scope="prototype"/>-->

4.javaConfig:

不需要xml了,直接由javaConfig管理

@Controller
public class User {
    @Value("阿三")
    private String name;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//Configuration代表这是一个配置类,beans.xml
//这个也会spring容器托管,注册到容器中,因为它本来就是一个@Component
@Configuration
public class MyConfig {
    @Bean
    public  User getUser(){
        return new User();
    }
}



public class MyTest {
    public static  void  main(String[] args){
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);

      User getUser = (User)context.getBean("getUser");
        System.out.println(getUser.getName());
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值