标签:
Spring-24 Adding Support for Annotation-Based Wiring
Namespace-->check ‘context‘-->In ‘context‘ Insert <context:annotation-config> element
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <bean id="logger" class="com.caveofprogramming.spring.test.Logger"> </bean> <bean id="consoleWriter1" class="com.caveofprogramming.spring.test.ConsoleWriter"> </bean> <bean id="fileWriter" class="com.caveofprogramming.spring.test.FileWriter"> </bean> <context:annotation-config></context:annotation-config> </beans>
Spring-25 The "Autowired" Annotation
@Autowired
method,variables,constuctor
Spring-26 Optional Beans
Question : What happened if we use @Autowired(required=false)?
Spring-27 Using qualifiers
Right click ‘consoleWriter‘--> extend ‘bean‘--->Insert <qualifier> element --->setValue ‘toConsole‘
@Autowired @Qualifier("toConsole") public void setConsoleWriter(ConsoleWriter writer) { this.consoleWriter = writer; }
还有一种用法
在method和class前分别添加@Qualifier???
Spring-28 The Resource Annotation(JSR-250)
@Resource
@Resource(name="banana")
这里的关于ambiguity有些问题?????
Spring-29 Annotation-Based Init and Destroy Methods
import.javax.annotation.*
@PostConstruct
@PreDestroy
Spring-30 The Inject Annotation(JSR-330)
javax.inject-1.jar (pom.xml)
public class Logger { private ConsoleWriter consoleWriter; private FileWriter fileWriter; /* * public Logger(ConsoleWriter consoleWriter, FileWriter fileWriter){ * this.consoleWriter = consoleWriter; this.fileWriter = fileWriter; */ @Inject @Named(value="consoleWriter") public void setConsoleWriter(ConsoleWriter writer) { this.consoleWriter = writer; } @Inject @Named(value="fileWriter") public void setFileWriter(FileWriter fileWriter) { this.fileWriter = fileWriter; } public void writeFile(String text) { fileWriter.write(text); } public void writeConsole(String text) { if (consoleWriter != null) { consoleWriter.write(text); } } @PostConstruct public void init(){ System.out.println("init"); } @PreDestroy public void destroy(){ System.out.println("destroy"); } }
Spring-31 Automatic Bean Discovery
<context:annotation-config></context:annotation-config> <context:component-scan base-package="com.caveofprogramming.spring.test"> </context:component-scan>
加上 @Component
为了不存在 ambiguity
1.@Component("fileWriter")
2.@Inject
@Named(value="fileWriter")
Spring-32 Setting Property Values via Annotations
@Value("")
标签:
原文地址:http://www.cnblogs.com/patrickspring/p/4914071.html