@autowired è usata per iniettare direttamente i valori configurati nell’xml nel bean.
Si puo usare sul metodo set, costruttore o direttamente sul campo.
Bean
Bean dichiarato nel file xml con un solo campo msg.
package com;
public class Message {
private String author;
private String body;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
public class Message {
private String author;
private String body;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
applicatioContext.xml
<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”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd”
xmlns:util=”http://www.springframework.org/schema/util”>
<context:component-scan base-package=”com” />
<bean id=”provaBean” class=”com.ProvaBean”>
</bean>
<bean id=”message” class=”com.Message”>
<property name=”msg” value=”Hello World” />
<property name=”author” value=”Pippo” />
</bean>
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:context=”http://www.springframework.org/schema/context”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd”
xmlns:util=”http://www.springframework.org/schema/util”>
<context:component-scan base-package=”com” />
<bean id=”provaBean” class=”com.ProvaBean”>
</bean>
<bean id=”message” class=”com.Message”>
<property name=”msg” value=”Hello World” />
<property name=”author” value=”Pippo” />
</bean>
</beans>
Abilitazione Annotazione
Per usare @autowired si deve registrare AutowiredAnnotationBeanPostProcessor. Questo si può fare in due modi:
1) si aggiunge
<context:annotation-config />
dentro applicationContext.xml
2) si aggiunge:
<bean class=”org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor”/> dentro applicationContext.xml
@autowired Esempi
@autowired su campo
Esecuzione Test
TestBean.java
@autowired su campo
ProvaBean.java
package com;
import org.springframework.beans.factory.annotation.Autowired;
public class ProvaBean {
@Autowired
private Message message;
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
}
import org.springframework.beans.factory.annotation.Autowired;
public class ProvaBean {
@Autowired
private Message message;
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
}
@autowired sul metodo set
ProvaBean.java
package com;
import org.springframework.beans.factory.annotation.Autowired;
public class ProvaBean {
private Message message;
public Message getMessage() {
return message;
}
@Autowired
public void setMessage(Message message) {
this.message = message;
}
}
import org.springframework.beans.factory.annotation.Autowired;
public class ProvaBean {
private Message message;
public Message getMessage() {
return message;
}
@Autowired
public void setMessage(Message message) {
this.message = message;
}
}
@autowired sul costruttore
ProvaBean.java
package com;
import org.springframework.beans.factory.annotation.Autowired;
public class ProvaBean {
private Message message;
@Autowired
public ProvaBean(Message msg)
{
message = msg;
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
}
import org.springframework.beans.factory.annotation.Autowired;
public class ProvaBean {
private Message message;
@Autowired
public ProvaBean(Message msg)
{
message = msg;
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
}
Esecuzione Test
TestBean.java
package com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
“applicationContext.xml”);
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBean {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
“applicationContext.xml”);
ProvaBean obj = (ProvaBean) context.getBean(“provaBean”);
System.out.println(obj.getMessage().getBody());
System.out.println(obj.getMessage().getAuthor());
}
}