1)@repository:注解在dao实现类上
2)@service:注解在service实现类上
3)@controller:注解在springmvc的处理器上
bean作用域:
@scope(prototype):用于指定对象创建模式,可以是单例模式或者原型模式,默认是singleton
基本类型属性注入:
@value
@autowired:bytype方式的注解式注入,即根据类型注解
@qualifier(myschool):byname方式的注解式注入,在使用@qualifier时必须与@autowired联合使用
域属性注解:
@resource:不加name属性则为bytype方式的注解式注入,但前提是注入的对象只能有一个
@resource(name=myschool):byname方式的注解式注入
bean的生命始末:
@postconstruct:当前bean初始化刚完毕
@predestroy:当前bean即将被销毁
@configuration:表示当前类充当spring容器,即所有的bean将由这个类来创建
注意:
在举例之前声明几个问题:
1、注解需要依赖spring-aop-4.3.9.release.jar包,所以需要导入依赖包。
2、使用注解方式注入,配置文件需要添加约束头文件:
<?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/spring-beans.xsd http://www.springframework.org/schema/context/spring-context.xsd">
也可以自己从spring的说明文档中找到此头文件:
3、如果使用到了springjunit4测试,则还需要导入spring-test-4.3.9.release.jar包
二、举例1、首先创建一个school类:
package com.ietree.spring.basic.annotation.demo1;import org.springframework.beans.factory.annotation.value;import org.springframework.stereotype.component; @component(myschool)public class school { @value(value = 清华大学)private string name;public school() {super(); }public school(string name) {super();this.name = name; }public void setname(string name) {this.name = name; } @overridepublic string tostring() {return school [name= + name + ]; } }
创建student类:
package com.ietree.spring.basic.annotation.demo1;import javax.annotation.postconstruct;import javax.annotation.predestroy;import javax.annotation.resource;import org.springframework.beans.factory.annotation.autowired;import org.springframework.beans.factory.annotation.qualifier;import org.springframework.beans.factory.annotation.value;import org.springframework.stereotype.component;/** * 说明: * 与@component注解功能相同,但意义不同的注解还有三个: * 1)@repository:注解在dao实现类上 * 2)@service:注解在service实现类上 * 3)@controller:注解在springmvc的处理器上 * * bean作用域: * @scope(prototype):用于指定对象创建模式,可以是单例模式或者原型模式,默认是singleton * * 基本类型属性注入: * @value * * @autowired:bytype方式的注解式注入,即根据类型注解 * @qualifier(myschool):byname方式的注解式注入,在使用@qualifier时必须与@autowired联合使用 * * 域属性注解: * @resource:不加name属性则为bytype方式的注解式注入,但前提是注入的对象只能有一个 * @resource(name=myschool):byname方式的注解式注入 * * bean的生命始末: * @postconstruct:当前bean初始化刚完毕 * @predestroy:当前bean即将被销毁 *///@scope(prototype)@component(mystudent)public class student { @value(value = 小明)private string name; @value(value = 25)private int age; // @autowired// @qualifier(myschool)// @resource(name=myschool) @resourceprivate school school;// 对象属性,也叫做域属性public student() {super(); } public student(string name, int age) {super();this.name = name;this.age = age; }public void setname(string name) { system.out.println(执行setname());this.name = name; }public void setage(int age) { system.out.println(执行setage());this.age = age; }public void setschool(school school) {this.school = school; } @overridepublic string tostring() {return student [name= + name + , age= + age + , school= + school + ]; } @postconstructpublic void initafter(){ system.out.println(当前bean初始化刚完毕); } @predestroypublic void predestroy(){ system.out.println(当前bean即将被销毁); } }
创建myjavaconfig类:
package com.ietree.spring.basic.annotation.demo1;import org.springframework.beans.factory.annotation.autowire;import org.springframework.context.annotation.bean;import org.springframework.context.annotation.configuration;/** * @configuration:表示当前类充当spring容器,即所有的bean将由这个类来创建 */@configurationpublic class myjavaconfig { @bean(name=myschool)public school myschoolcreator(){return new school(清华大学); } // autowire=autowire.by_type:指从当前类这个容器中查找与域属性的类型具有is-a关系的bean// autowire=autowire.by_name:指从当前类这个容器中查找与域属性同名的bean@bean(name=mystudent, autowire=autowire.by_type)public student mystudentcreator(){return new student(小明, 25); } }
创建配置文件:
<?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/spring-beans.xsd http://www.springframework.org/schema/context/spring-context.xsd"><!-- 扫描 com.ietree.spring.basic.annotation.demo1这个包及其子包 --><context:component-scan base-package="com.ietree.spring.basic.annotation.demo1"/><!-- 扫描 com.ietree.spring.basic这个包的子包 --><context:component-scan base-package="com.ietree.spring.basic.*"/></beans>
创建测试类:
package com.ietree.spring.basic.annotation.demo1;import org.junit.test;import org.junit.runner.runwith;import org.springframework.beans.factory.annotation.autowired;import org.springframework.context.applicationcontext;import org.springframework.context.support.classpathxmlapplicationcontext;import org.springframework.test.context.contextconfiguration;import org.springframework.test.context.junit4.springjunit4classrunner;@runwith(springjunit4classrunner.class) @contextconfiguration(locations=classpath:com/ietree/spring/basic/annotation/demo1/applicationcontext.xml)public class mytest { @autowiredprivate student student; @testpublic void test01() { string resource = com/ietree/spring/basic/annotation/demo1/applicationcontext.xml; applicationcontext ctx = new classpathxmlapplicationcontext(resource); school school = (school) ctx.getbean(myschool); system.out.println(school); student student = (student) ctx.getbean(mystudent); system.out.println(student); ((classpathxmlapplicationcontext)ctx).close(); } public void test02(){ system.out.println(student); } }
以上就是java中关于注解功能的详细介绍的详细内容。
