Spring 3 @autowired Esempio


@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;
}

}

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>

</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

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;
}


}

@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;
}


}


@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;
}


}



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”);

ProvaBean obj = (ProvaBean) context.getBean(“provaBean”);
System.out.println(obj.getMessage().getBody());
System.out.println(obj.getMessage().getAuthor());
}

}


avviando il main si otterrà, in tutte e tre casi, il seguente output:

Spring 3 Bean Example

Configurazione
Mettere il seguente file dentro la cartella resources.

applicationContext.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>
</beans>

Bean Spring
creare un file java di nome ProvaBean nel package “com”
ProvaBean.java
package com;
public class ProvaBean {
private String msg = “HelloWord”;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
creare un file java di nome TestBean:
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”);
ProvaBean obj = (ProvaBean) context.getBean(“provaBean”);
System.out.println(obj.getMsg());
}
}

Esecuzione
lanciare il main del file TestBean. L’ouput sarà:

Spring 3, get file properties from source with @Value

Creare un file di properties dentro la cartella resources:

configurationFBM.properties

prova= bohhhh

Modificare il file di configurazione di spring:

<beans xmlns:context=”http://www.springframework.org/schema/context” xmlns:util=”http://www.springframework.org/schema/util” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://www.springframework.org/schema/beans” 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”>

<context:component-scan base-package=”com.fbm”>
       <util:properties id=”configurationFBM” location=”classpath:/configurationFBM.properties”>

</util:properties></context:component-scan></beans>

Da dentro il codice si può accedere ad una proprietà del file di properties con la seguente annotation:

@Value(“#{configurationFBM[‘prova’]}”)
String someValue;

Install Spring 3 With Jar

Creare un file pom.xml cosi composto:

<project xmlns=”http://maven.apache.org/POM/4.0.0″ xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
  <modelVersion>4.0.0</modelVersion>
  <groupId>spring-source-download</groupId>
  <artifactId>SpringDependencies</artifactId>
  <version>1.0</version>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.5.RELEASE</version>
    </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
              <execution>
                <id>download-dependencies</id>
                  <phase>generate-resources</phase>
                    <goals>
                      <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                      <outputDirectory> ${project.build.directory}/dependencies </outputDirectory>
                    </configuration>
              </execution>
            </executions>
      </plugin>
    </plugins>
  </build>
</project>

cliccare con il tasto destro sopra il file e selezionare “install”.
Dentro la cartella target/dependencies ci saranno tuti i jar di Spring 3.