<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- <context:annotation-config/> -->
<context:component-scan base-package="shuai.spring.study"/>
</beans>
<context:component-scan base-package="shuai.spring.study"/>会自动扫描路径,自动注册需要注册的bean,而且,开启自动扫描会自动开始注解注入,就是上面注释掉的那句话。
Spring自带的注解实现bean定义:@Component注解及扩展@Repository、@Service、@Controller
JSR-250 1.1版本中中定义的@ManagedBean注解,是Java EE 6标准规范之一,不包括在JDK中,需要在应用服务器环境使用(如Jboss)
JSR-330的@Named注解
分析spring自带注解
@Repository:Dao层实现
@Service:Service层实现
@Controller:web层实现
这些注解都可以指定Bean的标识符:@Component("hello"),其实是@Component(value = "hello")
自定义扩展
package shuai.spring.study.zhujie;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component;
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface Shuai {
String value() default "";
}
其它的都不用动,注意,这个自定义注解上的@Component,还有里面的 String value() default "";,注意要指定为value,这样才能加参数
在需要的类名上加上注解就行了,@Shuai("hello")
其它看开涛老师的:http://jinnianshilongnian.iteye.com/blog/1461055