Spring Data Esempio Pratico
Spring Data permette di semplificare lo stato di persistenza rimuovendo completamente l’implementazione dei DAO dalla nostra applicazione. Per fare ciò, l’interfaccia DAO deve estendere JpaRepository e Spring Data creerà automaticamente un’implementazione dotata dei metodi CRUD più rilevanti per l’accesso ai dati.
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.
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
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>
@autowired su campo
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
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;
@Autowired
public ProvaBean(Message msg)
{
message = msg;
}
public Message getMessage() {
return message;
}
public void setMessage(Message message) {
this.message = message;
}
}
Esecuzione Test
TestBean.java
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());
}
}
Spring 3 Bean Example
Configurazione
Mettere il seguente file dentro la cartella resources.
applicationContext.xml
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>
Esecuzione
Spring 3, get file properties from source with @Value
Creare un file di properties dentro la cartella resources:
configurationFBM.properties
Modificare il file di configurazione di spring:
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:
String someValue;
Install Spring 3 With Jar
Creare un file pom.xml cosi composto:
<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.