Spting2.5後就可以使用Annotation的部分,在使用上面(較容易)可以取代原先XML的設定,但若是遇上Annotation和XML衝突的時候,則會以XML為主。
- Required (可使用在setter上)
Customer.java
package com.example ; import org.springframework.beans.factory.annotation.Required; public class Customer { private String name ; private int age ; public String getName() { return name; } @Required public void setName(String name) { this.name = name; } public int getAge() { return age; } @Required public void setAge(int age) { this.age = age; } }
Main.java
package com.example ; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Customer cust = (Customer) context.getBean("Customer"); System.out.println("name:"+cust.getName()) ; System.out.println("age:"+cust.getAge()) ; } }
設定檔必需加入<context:annotation-config/>,在Annotation有Required設定的情況下,若設定檔無設定的情況會造成BeanInitializationException: Property 'age' is required for bean 'Customer'
Beans.xml
<context:annotation-config/>
<!-- Definition for Customer bean -->
<bean id="Customer" class="com.example.Customer" >
<property name="name" value="Paul"/>
<!-- <property name="age" value="31"/> -->
</bean>
- Autowired (可使用在setter、constructor和property上)
1.setter
Customer.java
package com.example1; import org.springframework.beans.factory.annotation.Autowired; public class Customer { private Address address; public Address getAddress() { return address; } @Autowired public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Customer [address=" + address + "]"; } }
Address.java
package com.example1; public class Address { private String fulladdress; public String getFulladdress() { return fulladdress; } public void setFulladdress(String fulladdress) { this.fulladdress = fulladdress; } @Override public String toString() { return "Address [fulladdress=" + fulladdress + "]"; } }
Main.java
package com.example1; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "Beans1.xml"); Customer cust = (Customer) context.getBean("customer"); System.out.println(cust); } }
Beans1.xml
<context:annotation-config/>
<bean id="customer" class="com.example1.Customer" />
<bean id="address" class="com.example1.Address" >
<property name="fulladdress" value="Taipei, TW" />
</bean>
結果顯示如下所示:
Customer [address=Address [fulladdress=Taipei, TW]]
2.constructor
Developer.java
package com.example2; import org.springframework.beans.factory.annotation.Autowired; public class Developer { private Language language; @Autowired public Developer(Language language) { this.language = language; } @Override public String toString() { return "Developer [language=" + language + "]"; } }
Language.java
package com.example2; public class Language { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Language [name=" + name + "]"; } }
Main.java
package com.example2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "Beans2.xml"); Developer developer = (Developer) context.getBean("developer"); System.out.println(developer); } }
Beans2.xml
<context:annotation-config/>
<bean id="developer" class="com.example2.Developer"/>
<bean id="language" class="com.example2.Language" >
<property name="name" value="Java" />
</bean>
結果顯示如下所示:
Developer [language=Language [name=Java]]
3.property
Person.java
package com.example3; import org.springframework.beans.factory.annotation.Autowired; public class Person { @Autowired private Ability ability; public Ability getAbility() { return ability; } public void setAbility(Ability ability) { this.ability = ability; } @Override public String toString() { return "Person [ability=" + ability + "]"; } }
Ability.java
package com.example3; public class Ability { private String skill; public String getSkill() { return skill; } public void setSkill(String skill) { this.skill = skill; } @Override public String toString() { return "Ability [skill=" + skill + "]"; } }
Main.java
package com.example3; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "Beans3.xml"); Person person = (Person) context.getBean("person"); System.out.println(person); } }
Beans3.xml
<context:annotation-config/>
<bean id="person" class="com.example3.Person" />
<bean id="invisible" class="com.example3.Ability" >
<property name="skill" value="Invisible" />
</bean>
結果顯示如下所示:
Person [ability=Ability [skill=Invisible]]
另外,@Autowired(required=false)的設定會使得抓到參數的值為default值。
Customer.java
package com.example ; import org.springframework.beans.factory.annotation.Required; import org.springframework.beans.factory.annotation.Autowired; public class Customer { private String name ; private int age ; public String getName() { return name; } @Required public void setName(String name) { this.name = name; } public int getAge() { return age; } @Autowired(required=false) public void setAge(int age) { this.age = age; } }
Main.java
package com.example ; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Customer cust = (Customer) context.getBean("Customer"); System.out.println("name:"+cust.getName()) ; System.out.println("age:"+cust.getAge()) ; } }
Beans.xml
<context:annotation-config/>
<!-- Definition for Customer bean -->
<bean id="Customer" class="com.example.Customer" >
<property name="name" value="Paul"/>
<!-- <property name="age" value="31"/> -->
</bean>
**結果顯示如下所示:
name:Paul
age:0
- Qualifier
使用上需和Autowired一起使用,主要是用來確認何種bean需要被注入。
Customer.java
package com.example4; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class Customer { @Autowired @Qualifier("address1") private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Customer [address=" + address + "]"; } }
Address.java
package com.example4; public class Address { private String fulladdress; public String getFulladdress() { return fulladdress; } public void setFulladdress(String fulladdress) { this.fulladdress = fulladdress; } @Override public String toString() { return "Address [fulladdress=" + fulladdress + "]"; } }
Main.java
package com.example4; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "Beans4.xml"); Customer cust = (Customer) context.getBean("customer"); System.out.println(cust); } }
Beans4.xml
<context:annotation-config/>
<bean id="customer" class="com.example4.Customer" />
<bean id="address1" class="com.example4.Address" >
<property name="fulladdress" value="Taipei, TW" />
</bean>
<bean id="address2" class="com.example4.Address" >
<property name="fulladdress" value="New York, American" />
</bean>
結果顯示如下所示:
Customer [address=Address [fulladdress=Taipei, TW]]
- PostConstruct and PreDestroy
可針對物件初始化和物件銷毀做annotation
HelloWorld.java
package com.example5 ; import javax.annotation.*; public class HelloWorld { private String message ; @PostConstruct public void init(){ System.out.println("Initialization now") ; } public void getMessage(){ System.out.println("Your Message : " + message); } public void setMessage(String message) { this.message = message; } @PreDestroy public void destroy(){ System.out.println("Destory now") ; } }
Main.java
package com.example5 ; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans5.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); context.registerShutdownHook(); } }
Beans5.xml
<context:annotation-config/>
<bean id="helloWorld" class="com.example5.HelloWorld" init-method="init" destroy-method="destroy">
<property name="message" value="Hello World!"/>
</bean>
結果顯示如下所示:
Initialization now
Your Message : Hello World!
Destory now
- Configuration and Bean
利用Configuration and Bean Annotation可以不需使用設定檔。
HelloWorldConfig.java
package com.example6 ; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class HelloWorldConfig { @Bean public HelloWorld helloWorld(){ return new HelloWorld(); } @Bean public HelloWorld1 helloWorld1(){ return new HelloWorld1(); } }
上述程式碼的設定等同於
<beans>
<bean id="helloWorld" class="com.example6.HelloWorld" />
</beans>
<beans>
<bean id="helloWorld1" class="com.example6.HelloWorld1" />
</beans>
HelloWorld.java
package com.example6 ; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public String getMessage(){ return message ; } }
HelloWorld1.java
package com.example6 ; public class HelloWorld1 { private String message1; public void setMessage(String message1){ this.message1 = message1; } public String getMessage(){ return message1 ; } }
Main.java
package com.example6 ; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(HelloWorldConfig.class); HelloWorld obj = (HelloWorld) ctx.getBean(HelloWorld.class); obj.setMessage("Hello world"); System.out.println(obj.getMessage()) ; HelloWorld1 obj1 = (HelloWorld1) ctx.getBean(HelloWorld1.class); obj1.setMessage("Hello world1"); System.out.println(obj1.getMessage()) ; } }
結果顯示如下所示:
Hello world
Hello world1
- import
@Configuration public class ConfigA { @Bean public A a() { return new A(); } }
@Configuration @Import(ConfigA.class) public class ConfigB { @Bean public B a() { return new A(); } }
public static void main(String[] args) { ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigB.class); A a = ctx.getBean(A.class); B b = ctx.getBean(B.class); }
- init-method and destroy-method
public class Foo { public void init() { // initialization logic } public void cleanup() { // destruction logic } } @Configuration public class AppConfig { @Bean(initMethod = "init", destroyMethod = "cleanup" ) public Foo foo() { return new Foo(); } }
- scope
@Configuration public class AppConfig { @Bean @Scope("prototype") public Foo foo() { return new Foo(); } }
留言列表