Skip to content

Commit

Permalink
Add log4j configration.
Browse files Browse the repository at this point in the history
Add Spring Integration Settings.
  • Loading branch information
heki1224 committed Jan 10, 2013
1 parent 946d767 commit 29426db
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 39 deletions.
50 changes: 46 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>3.2.0.RELEASE</org.springframework.version>
<org.slf4j.version>1.7.2</org.slf4j.version>
</properties>

Expand Down Expand Up @@ -39,6 +40,40 @@
</build>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-configuration</groupId>
Expand All @@ -49,14 +84,16 @@
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<artifactId>commons-lang</artifactId>
<groupId>commons-lang</groupId>
</exclusion>
</exclusions>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.11</version>
</dependency>

<dependency>
<groupId>javax.jdo</groupId>
<artifactId>jdo-api</artifactId>
Expand Down Expand Up @@ -378,6 +415,11 @@
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

<dependency>
<groupId>junit</groupId>
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/sample/datanucleus_sample/dao/ProductDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package sample.datanucleus_sample.dao;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import sample.datanucleus_sample.model.MyClass;

@Repository
public class ProductDao {
@Autowired
private PersistenceManagerFactory persistenceManagerFactory;

public Object loadProductsByCategory(String category) {
PersistenceManager pm = this.persistenceManagerFactory.getPersistenceManager();
try {
Query query = pm.newQuery(MyClass.class);
List<MyClass> results = (List<MyClass>) query.execute();
return results.get(0);
} finally {
pm.close();
}
}

@Transactional
public MyClass save(MyClass input) {
PersistenceManager pm = this.persistenceManagerFactory.getPersistenceManager();
try {
return pm.makePersistent(input);
} finally {
pm.close();
}
}
}
35 changes: 0 additions & 35 deletions src/main/java/sample/datanucleus_sample/main/Main.java

This file was deleted.

30 changes: 30 additions & 0 deletions src/main/resources/log4j.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="threshold" value="DEBUG" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d\t%5p %C - %m%n" />
</layout>
</appender>

<appender name="file" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="sample.log" />
<param name="Encoding" value="UTF-8" />
<param name="threshold" value="DEBUG" />
<param name="Append" value="true" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p: %l - %m%n" />
</layout>
</appender>

<root>
<priority value="DEBUG" />
<appender-ref ref="file" />
<appender-ref ref="STDOUT" />
</root>

</log4j:configuration>
25 changes: 25 additions & 0 deletions src/main/resources/spring/app-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<!-- Scans within the base package of the application for @Components to
configure as beans -->
<context:component-scan base-package="sample.datanucleus_sample" />
<context:property-placeholder location="classpath:datanucleus.properties" />

<!-- Apache Commons Configuration Composite configuration -->
<bean id="configuration" class="org.apache.commons.configuration.CompositeConfiguration">
<constructor-arg>
<list>
<bean class="org.apache.commons.configuration.PropertiesConfiguration">
<constructor-arg type="java.net.URL">
<value>classpath:datanucleus.properties</value>
</constructor-arg>
</bean>
</list>
</constructor-arg>
</bean>
</beans>
23 changes: 23 additions & 0 deletions src/main/resources/spring/db-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<!-- DBの設定を外だしにするための設定ファイルの定義 -->
<bean id="pmf"
class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean">
<property name="configLocation" value="classpath:datanucleus.properties" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jdo.JdoTransactionManager">
<property name="persistenceManagerFactory" ref="pmf" />
</bean>

<tx:annotation-driven />

</beans>
52 changes: 52 additions & 0 deletions src/test/java/sample/datanucleus_sample/dao/ProductDaoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package sample.datanucleus_sample.dao;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import sample.datanucleus_sample.model.MyClass;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"file:src/main/resources/spring/app-config.xml",
"file:src/main/resources/spring/db-config.xml" })
public class ProductDaoTest {

@Autowired
private ProductDao dao;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
}

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testLoadProductsByCategory() {
MyClass actual = (MyClass) dao.loadProductsByCategory("firstName");
assertThat(actual.getId(), is(10L));

actual.setId(101L);
dao.save(actual);
}

}

0 comments on commit 29426db

Please sign in to comment.