spring开发_Annotation注解介绍



spring开发Annotation注解介绍。

项目结构:

http://www.cnblogs.com/hongten/gallery/image/112614.html

/spring_1800_Annotation/src/com/b510/app/test/SpringTest.java

复制代码
1 package com.b510.app.test;
2
3 import java.util.Arrays;
4
5 import org.springframework.context.ApplicationContext;
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8 /**
9 * 测试类
10 *
11 * @author Hongten
12 *
13 */
14 public class SpringTest {
15 public static void main(String[] args) {
16 ApplicationContext act = new ClassPathXmlApplicationContext(“beans.xml”);
17 System.out.println(Arrays.toString(act.getBeanDefinitionNames()));
18 }
19 }
复制代码
/spring_1800_Annotation/src/com/b510/service/AnimalService.java

复制代码
1 package com.b510.service;
2
3
4 /**
5 * 动物抽象类
6 *
7 * @author Hongten
8 *
9 */
10 public interface AnimalService {
11
12 /**
13 * 获取相关信息
14 */
15 public abstract void getInfo();
16
17 }
复制代码
/spring_1800_Annotation/src/com/b510/service/MeatService.java

复制代码
1 package com.b510.service;
2
3 /**
4 * 定义抽象类MeatService肉类
5 *
6 * @author Hongten
7 *
8 */
9 public interface MeatService {
10
11 /**
12 * 定义方法whatMeat
13 *
14 * @return 返回一个字符串
15 */
16 public abstract String whatMeat();
17 }
复制代码
采用@Component和采用@Component(“cat”)有什么区别:

采用第一种方式添加注解,Spring会采用约定的方式来为这些Bean实例指定名称,这些Bean实例的名称默认是Bean类的首字母小写,其他部分不变。如:catServiceBean

而采用第二种方式添加注解,相当于覆盖了Spring提供的默认实例名称,所以Bean的实例名称为:cat


/spring_1800_Annotation/src/com/b510/service/impl/CatServiceBean.java

复制代码
1 package com.b510.service.impl;
2
3 import javax.annotation.PostConstruct;
4 import javax.annotation.Resource;
5
6 import org.springframework.stereotype.Component;
7
8 import com.b510.service.AnimalService;
9 import com.b510.service.MeatService;
10
11 /**
12 * 定义猫类实现AnimaleService抽象类
13 *
14 * @author Hongten
15 *
16 */
17 @Component(“cat”)
18 public class CatServiceBean implements AnimalService {
19 private int age;
20 private MeatService meatService;
21
22 public CatServiceBean(){
23 System.out.println(“猫类被初始化了”);
24 }
25
26 public int getAge() {
27 return age;
28 }
29
30 //指定出示化方法:@PostConstruct
31 @PostConstruct
32 @Override
33 public void getInfo() {
34 System.out.println(“我是猫,我的年龄是:”+age+”,我很喜欢吃”+meatService.whatMeat());
35 }
36 public MeatService getMeatService() {
37 return meatService;
38 }
39
40 public void setAge(int age) {
41 this.age = age;
42 }
43 @Resource(name=”fish”)
44 public void setMeatService(MeatService meatService) {
45 this.meatService = meatService;
46 }
47
48
49 }
复制代码
/spring_1800_Annotation/src/com/b510/service/impl/DogServiceBean.java

复制代码
1 package com.b510.service.impl;
2
3 import javax.annotation.PostConstruct;
4 import javax.annotation.PreDestroy;
5 import javax.annotation.Resource;
6
7 import org.springframework.stereotype.Component;
8
9 import com.b510.service.AnimalService;
10 import com.b510.service.MeatService;
11
12 /**
13 * 定义DogServiceBean类
14 *
15 * @author Hongten
16 *
17 */
18 @Component(“dog”)
19 public class DogServiceBean implements AnimalService {
20 private int age;
21 // 修饰Field的时候,setter方法可以不要滴
22 @Resource(name = “pork”)
23 private MeatService meatService;
24
25 public DogServiceBean() {
26 System.out.println(“狗类被初始化了”);
27 }
28
29 public int getAge() {
30 return age;
31 }
32
33 // 指定的出示化方法
34 @PostConstruct
35 @Override
36 public void getInfo() {
37 System.out.println(“我是狗,我的年龄是:” + age + “,我很喜欢吃”
38 + meatService.whatMeat());
39 }
40
41 public MeatService getMeatService() {
42 return meatService;
43 }
44
45 public void setAge(int age) {
46 this.age = age;
47 }
48
49 // 指定Bean销毁之前的方法
50 @PreDestroy
51 public void close() {
52 System.out.println(“这是Bean DogServiceBean销毁之前的方法”);
53 }
54 }
复制代码
/spring_1800_Annotation/src/com/b510/service/impl/FishServiceBean.java

复制代码
1 package com.b510.service.impl;
2
3 import org.springframework.stereotype.Component;
4
5 import com.b510.service.MeatService;
6
7 /**
8 * 定义鱼肉实现MeatService抽象类
9 *
10 * @author Hongten
11 *
12 */
13 @Component(“fish”)
14 public class FishServiceBean implements MeatService {
15
16 public FishServiceBean(){
17 System.out.println(“鱼肉类被初始化了”);
18 }
19 @Override
20 public String whatMeat() {
21 return “鱼肉”;
22 }
23
24 }
复制代码
/spring_1800_Annotation/src/com/b510/service/impl/PorkServiceBean.java

复制代码
1 package com.b510.service.impl;
2
3 import org.springframework.stereotype.Component;
4
5 import com.b510.service.MeatService;
6
7 /**
8 * 定义猪肉实现MeatService抽象类
9 *
10 * @author Hongten
11 *
12 */
13 @Component(“pork”)
14 public class PorkServiceBean implements MeatService {
15
16 public PorkServiceBean(){
17 System.out.println(“猪肉类被初始化了”);
18 }
19 @Override
20 public String whatMeat() {
21 return “猪肉”;
22 }
23
24 }
复制代码
/spring_1800_Annotation/src/beans.xml

复制代码
1 <?xml version=”1.0″ encoding=”UTF-8″?>
2 <beans xmlns=”http://www.springframework.org/schema/beans”
3 xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:context=”http://www.springframework.org/schema/context”
4 xmlns:aop=”http://www.springframework.org/schema/aop” xmlns:tx=”http://www.springframework.org/schema/tx”
5 xsi:schemaLocation=”http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
9 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd”>
10 <!– 自动扫描指定包及其子包的所有Bean类 –>
11 <context:component-scan base-package=”com.b510.service”></context:component-scan>
12 <bean id=”cat” class=”com.b510.service.impl.CatServiceBean”>
13 <property name=”age” value=”2″></property>
14 </bean>
15 <bean id=”dog” class=”com.b510.service.impl.DogServiceBean”>
16 <property name=”age” value=”5″></property>
17 </bean>
18 </beans>
复制代码
运行结果:

复制代码
1 2012-3-12 21:17:39 org.springframework.context.support.AbstractApplicationContext prepareRefresh
2 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]; startup date [Mon Mar 12 21:17:39 CST 2012]; root of context hierarchy
3 2012-3-12 21:17:39 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
4 信息: Loading XML bean definitions from class path resource [beans.xml]
5 2012-3-12 21:17:39 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
6 信息: Overriding bean definition for bean ‘cat’: replacing [Generic bean: class [com.b510.service.impl.CatServiceBean]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\WordPlace\spring_workspace\spring_1800_Annotation\bin\com\b510\service\impl\CatServiceBean.class]] with [Generic bean: class [com.b510.service.impl.CatServiceBean]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [beans.xml]]
7 2012-3-12 21:17:39 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
8 信息: Overriding bean definition for bean ‘dog’: replacing [Generic bean: class [com.b510.service.impl.DogServiceBean]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [D:\WordPlace\spring_workspace\spring_1800_Annotation\bin\com\b510\service\impl\DogServiceBean.class]] with [Generic bean: class [com.b510.service.impl.DogServiceBean]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [beans.xml]]
9 2012-3-12 21:17:39 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
10 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListableBeanFactory@587c94
11 2012-3-12 21:17:39 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
12 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@587c94: defining beans [cat,dog,fish,pork,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor]; root of factory hierarchy
13 猫类被初始化了
14 鱼肉类被初始化了
15 我是猫,我的年龄是:2,我很喜欢吃鱼肉
16 狗类被初始化了
17 猪肉类被初始化了
18 我是狗,我的年龄是:5,我很喜欢吃猪肉
19 [cat, dog, fish, pork, org.springframework.context.annotation.internalCommonAnnotationProcessor, org.springframework.context.annotation.internalAutowiredAnnotationProcessor, org.springframework.context.annotation.internalRequiredAnnotationProcessor]
复制代码

http://www.cnblogs.com/hongten/archive/2012/03/12/java_spring_annotation.html

http://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=93197193_hao_pg&wd=spring%20anotation&rsv_pq=a078c0b500014107&rsv_t=0169zlRrbWhWmy2M0ZjXt25sMZ%2BelGisy4mCcc0VR6V2O%2BNj6UWu&rsv_enter=0&inputT=838&rsv_sug3=36&rsv_sug4=2405&rsv_sug1=3&rsv_sug2=0