注解
声明Bean的注解
@Component 没有明确的角色
@Service 在业务逻辑层(Service层)
@Reponsitory在数据访问层(dao层)
@Controller在展现层(mvc)使用
注入Bean注解
@Autowired:spring提供的注解
其他注解
@Configuration声明当前类是一个配置类
@ComponentScan自动扫描报下所有使用@service @component @repository @controller 并注册为Bean
@Bean 注解声明当前方法返回值是一个bean
@value 注入普通字符串,操作系统属性,网址资源等
注解的使用
全局配置使用java配置(如数据库相关配置,MVC相关配置。@Configuration @Bean)。
业务Bean的配置使用疏解配置(@Service @Component @Respository @controller)
实例
@Service 声明当前fun类是spring管理的一个bean
public class fun{
@Value("${book.name}")
private String name;
@Autowired
fun2 f; //将fun2的实体类bean注入到fun中,让fun具备fun2的功能;
public String s(){
return “s”;
}
}