java中spring的注解_Java代码中spring注解浅析

在项目配置中,用注解代替配置,已经很普遍了。

在spring中,有看到Component,Repository,Service,Controller

在研究这四个注解之前,需要先了解ClassPathBeanDefinitionScanner

我们在application.xml配置的

后台处理的bean就是这个类。以下是源代码的描述。

其中这个类的属性BeanDefinitionDefaults beanDefinitionDefaults,管理了spring容器处理这些bean的方式。如是否支持延时加载,自动匹配规则,初始化方法,销毁方法等。

Java代码

/**

* A bean definition scanner that detects bean candidates on the classpath,

* registering corresponding bean definitions with a given registry (BeanFactory

* or ApplicationContext).

*

*

Candidate classes are detected through configurable type filters. The

* default filters include classes that are annotated with Spring's

* {@link org.springframework.stereotype.Component @Component},

* {@link org.springframework.stereotype.Repository @Repository},

* {@link org.springframework.stereotype.Service @Service}, or

* {@link org.springframework.stereotype.Controller @Controller} stereotype.

*

* @author Mark Fisher

* @author Juergen Hoeller

* @since 2.5

* @see org.springframework.stereotype.Component

* @see org.springframework.stereotype.Repository

* @see org.springframework.stereotype.Service

* @see org.springframework.stereotype.Controller

*/

Java代码

package car;

public interface Car {

public String getCarName();

}

Java代码

package car;

import org.springframework.stereotype.Service;

//@Service

public class Audi implements Car{

@Override

public String getCarName() {

return "AUDI";

}

}

Java代码

package car;

import org.springframework.stereotype.Service;

@Service

public class Bmw implements Car{

@Override

public String getCarName() {

return "BMW";

}

}

Java代码

package car;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

@Service

public class User {

@Autowired

//@Qualifier("audi")

private Car carssssss;

public String getCar() {

return carssssss.getCarName();

}

}

Java代码

package car;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {

System.out.println("");

ApplicationContext context = new ClassPathXmlApplicationContext("car/test.xml");

String[] names = context.getBeanDefinitionNames();

for (int i = 0; i < names.length; i++) {

System.out.println(names[i]);

}

}

}

上面的测试类main方法,并没有报错,至少可以看出两点:

(1)通过Service注解,注册到spring容器的名字是类名首字小写。

(2)用Autowired注解,因为User类,属性名是瞎写的,但依然可以注入成功,说明 默认是根据类型进行注入的。

(3)把Audi类中的注解打开,就会抛异常,是因为spring容器有两个相同的类型,spring容器不知道应该注入哪个类。出现这种场合,需要@Qualifier("audi")来指定用哪个类来进行注入。这时autowired注入方式,由byType转成byName.

(4)@Resource 的作用相当于 @Autowired,只不过 @Autowired 按 byType 自动注入,面 @Resource 默认按 byName 自动注入罢了。@Resource 有两个属性是比较重要的,分别是 name 和 type,Spring 将 @Resource 注释的 name 属性解析为 Bean 的名字,而 type 属性则解析为 Bean 的类型。所以如果使用 name 属性,则使用 byName 的自动注入策略,而使用 type 属性时则使用 byType 自动注入策略。如果既不指定 name 也不指定 type 属性,这时将通过反射机制使用 byName 自动注入策略。

打印结果:

bmw

user

org.springframework.context.annotation.internalConfigurationAnnotationProcessor

org.springframework.context.annotation.internalAutowiredAnnotationProcessor

org.springframework.context.annotation.internalRequiredAnnotationProcessor

org.springframework.context.annotation.internalCommonAnnotationProcessor

org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor

org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor

63bdaf3eb047280669e26d303b4959ae.png

从上图,用Component,Repository,Service,Controller四个注解,都可以把类注册到spring容器里,但这四个有什么区别呢。使用上没什么特别的,都可以把类注册到spring中。但是四个注解暗示类代表着不同的含义.

1、@Service用于标注业务层组件

2、@Controller用于标注控制层组件(如struts中的action)

3、@Repository用于标注数据访问组件,即DAO组件.

4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

具体信息可以参考源代码关于注解的描述。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值