Java Reference
In-Depth Information
<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.0xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd " >
<context:load-time-weaver />
<context:annotation-config />
<context:spring-configured />
<bean id= " dataSource "
class= " org.springframework.jdbc.datasource.DriverManagerDataSource " >
<property name= " driverClassName "
value= " org.apache.derby.jdbc.ClientDriver " />
<property name= " url "
value= " jdbc: derby://localhost:1527/bookshop; create=true " />
<property name= " username " value= " app " />
<property name= " password " value= " app " />
</bean>
<bean id= " jdbcTemplate "
class= " org.springframework.jdbc.core.JdbcTemplate " >
<property name= " dataSource " ref= " dataSource " />
</bean>
</beans>
In this bean configuration file, you can define a JDBC template on a data source and then it will be
auto-wired into book domain objects for them to access the database.
Now you can create the following Main class to test this domain class. Of course, there's no
transaction support at this moment.
package com.apress.springenterpriserecipes.bookshop.aspectj;
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 " );
Book book = new Book( " 0001 " , " My First Book " , 30);
book.purchase( " user1 " );
}
}
For a simple Java application, you can weave this aspect into your classes at load time with the
Spring agent specified as a VM argument.
Search WWH ::




Custom Search