@Required注释

作用:用于属性的set方法,那么这个属性必须在xml文件的bean标签里面进行配置,否则就会抛出一个BeanInitializationException异常。

首先准备一个类:

public class Person{
private int age;
private String name;
@Required
public void setAge(int age) {
this.age = age;
}
public Integer getAge() {
return age;
}
@Required
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

再准备一个测试类:

public class MainApp{
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("person.xml");
Person person = (Person) context.getBean("p");
System.out.println("Name : " + student.getName() );
System.out.println("Age : " + student.getAge() );
}
}

配置文件内容如下:

<?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/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
   //必须加上这个标签,后面几个注释的配置文件也一样。 
<context:annotation-config/> <bean id="p" class="Annotations.injection.Person">
<property name="name" value="张三 />
<property name="age" value="18"/>
</bean>
</beans>

大家可以自行把<property name="age" value="11"/>或者 <property name="name" value="Zara" />注释掉,看看是否会报错。上面的代码才是完整的。

输出结果:

Name : 张三
Age : 11

@Autowired

作用:这个标签在不同的部位前面,作用也不一样。但是总的来说,作用基本和其名字一样,自动连线,只是连线对象不一样了。

当在一个set方法前,即使我们没在bean里面配置他的值或引用,它也会在beans里面寻找相同类型的bean去匹配,就如byType一样。

当在一个属性前,这个属性可以不需要set方法。

当在一个构造函数前,尤其是有参构造函数,即使我们不给这个构造函数传参,它也会在beans里寻找相同类型的bean,并传递给这个构造函数。

下面分别演示和对比其作用。

在set方法前

首先准备一个类,在其set方法前加上Autowired注释:

public class Hello {
private Hello_Son hs;
public Hello_Son getHs() {
return hs;
}
@Autowired
public void setHs(Hello_Son hs) {
this.hs = hs;
}
}

为了方便演示,再准备一个类当自定义类型:

public class Hello_Son {
public Hello_Son(){
System.out.println("这是hello_son的无参构造函数");
}
}

测试类:

public class MainApp {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("ann_bean.xml");
Hello h=(Hello)context.getBean("hello");
h.getHs();
}
}

区别对比:

使用了Autowired注释的配置文件内容如下:

<?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/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="hello" class="Annotations.injection.Hello">
</bean>
<bean id="hs" class="Annotations.injection.Hello_Son"/>
</beans>

不使用Autowired注释的配置文件内容如下:

<?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/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="hello" class="Annotations.injection.Hello">
<!-- 需要手动配置-->
<property name="hs" ref="hs"></property>
</bean>
<bean id="hs" class="Annotations.injection.Hello_Son"/>
</beans>

运行结果:

这是hello_son的无参构造函数

在属性前(在set方法前和在属性前结果一样,所以一般用这个)

首先准备一个类,无需set方法:

public class Hello {
@Autowired
private Hello_Son hs; public Hello_Son getHs() {
return hs;
} }

自定义类型类和上面一样。

配置文件和上面一样。

区别对比:

使用了Autowired注释的属性,不需要set方法。

不使用Autowired注释的属性,需要set方法。

运行结果:

这是hello_son的无参构造函数

在构造函数前

首先准备一个类:

public class Hello {
private Hello_Son hs;
@Autowired
public Hello(Hello_Son hs) {
System.out.println("这是hello的有参构造函数");
this.hs=hs;
}
public Hello_Son getHs() {
return hs;
}
}

自定义类型类和配置文件不变。

区别对比:

使用了Autowired注释的构造函数的配置文件:

<?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/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="hello" class="Annotations.injection.Hello">
</bean>
<bean id="hs" class="Annotations.injection.Hello_Son"/>
</beans>

不使用Autowired注释的构造函数的配置文件:

<?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/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="hello" class="Annotations.injection.Hello">
<constructor-arg ref="hs"/>
</bean>
<bean id="hs" class="Annotations.injection.Hello_Son"/>
</beans>

运行结果:

这是hello_son的无参构造函数
这是hello的有参构造函数

@Qualifier

作用:当创建多个相同类型的bean时,在使用时,只需要配置其中一个,那么这时候就可以使用@Qualifier注释。

首先创建一个类:

public class students {
private int age;
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} public int getAge() {
return age;
}
}

再为学生类创建一个配置类,方便使用:

public class stu_profile {
@Autowired
@Qualifier("stu1")
private students stu; public stu_profile() {
System.out.println("配置类的构造函数");
} public void getStu() {
System.out.println("名字叫:" + stu.getName() + ";" + "年龄:" + stu.getAge());
}
}

可以看到,配置类中指定了stu1,这个stu1就是一个bean的id。并和@Autowired一起使用,这样就不用再写个set方法了。

配置文件内容如下:

<?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/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/>
<bean id="profile" class="Annotations.injection.stu_profile"></bean>
<bean id="stu1" class="Annotations.injection.students">
<property name="age" value="18"/>
<property name="name" value="张三"/>
</bean>
<bean id="stu2" class="Annotations.injection.students">
<property name="name" value="李四"/>
<property name="age" value="19"/>
</bean> </beans>

测试类:

public class MainApp {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("students.xml");
stu_profile stu=(stu_profile)context.getBean("profile");
stu.getStu();
}
}

运行结果:

配置类的构造函数
名字叫:张三;年龄:18

Spring基于注解的配置1——@Required、@Autowired、@Qualifier示例及与传统注入方法的对比的更多相关文章

  1. Spring 基于注解零配置开发

    本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...

  2. (spring-第4回【IoC基础篇】)spring基于注解的配置

    基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...

  3. Spring 基于注解的配置 简介

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  4. Spring基于注解的配置概述

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration.html: 从Spring 2.5开始 ...

  5. java Spring 基于注解的配置(一)

    注解引用:1.service.xml 配置注解模式 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  6. Spring基于注解@Required配置

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  7. Spring IoC — 基于注解的配置

    基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...

  8. Spring框架bean的配置(3):基于注解的配置

    1.基于注解的配置: @Component: 基本注解, 标识了一个受 Spring 管理的组件 @Respository: 标识持久层组件 @Service: 标识服务层(业务层)组件 @Contr ...

  9. 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置

    复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...

随机推荐

  1. printf 格式输出代码大全

    d,lx,ld,,lu,这几个都是输出32位的hd,hx,hu,这几个都是输出16位数据的,hhd,hhx,hhu,这几个都是输出8位的,lld,ll,llu,llx,这几个都是输出64位的, pri ...

  2. Java基础(十一)回调(callback)与对象克隆(Cloneable)

    一.回调 1.回调是一种常见的程序设计模式,可以指出某个特定时间发生时应该采取的动作. 在java.swing包中有一个类Timer类,可以使用它在到达指定的时间间隔作出什么动作.那么就有两个问题,即 ...

  3. IIS中如何设置域名

    如何在IIS中设置域名: 1,想好我们想要配置的本地域名,我们以www.baidu.com为例. 2,打开系统盘,一般默认的系统盘为C盘,打开:C:\Windows\System32\drivers\ ...

  4. [考试反思]1012csp-s模拟测试70:盘旋

    这套题比较烂... 上来看到T2是原题,一想上一次考试遇到原题就不换,这次应该也是,于是直接开始码,码了一半然后换题了 T1打表找规律或者推式子都不难... T2水的一匹暴力剪枝即可,但是我并不知道数 ...

  5. 差异:后缀数组(wzz模板理解),单调栈

    因为涉及到对模板的理解,所以就着代码看会好一些. 让那些坚决不颓代码的人受委屈了. 我是对着wzz的板子默写的,可能不完全一样啊. 还有代码注释里都是我个人的理解,不保证正确,但欢迎指正. 可以有选择 ...

  6. 使用Typescript重构axios(十三)——让响应数据支持泛型

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  7. BZOJ 2101: [Usaco2010 Dec]Treasure Chest 藏宝箱(这是我写过最骚气的dp!)

    题目描述 贝西和邦妮找到了一个藏宝箱,里面都是金币! 但是身为两头牛,她们不能到商店里把金币换成好吃的东西,于是她们只能用这些金币来玩游戏了.   藏宝箱里一共有N枚金币,第i枚金币的价值是Ci.贝西 ...

  8. day 2 DP专场

    上午讲了一上午背包,从01背包到完全背包到多重背包,感觉也没说什么,旁边的大佬一直在飞鸽里说让老师讲快点,然而最后也没人敢跟老师说.... 例题真的各个都是神仙题, 挂饰 好像就是一上午最简单的... ...

  9. Android Studio接谷歌原生登录

    目录 前言 AndroidStudio server_client_id @ 前言 准备 近日,公司要求上线海外市场,需要接入海外SDK,首先上架的是GooglePlay,需要先接入GooglePla ...

  10. JDK下载安装配置教程(详细)

    JDK下载安装配置教程(详细) 版权声明:本文为原创文章,转载请附上原文出处链接和本声明.https://www.cnblogs.com/mxxbc/p/11844885.html 因为最近需要在Wi ...