上一个,我们介绍了如何通过Seasar2来实现Bean的管理调用,上一个是基于app.icon配置文件做的,我这次教大家如何做自动注入的方式来管理Bean,需要配置j2ee.dicon、app.dicon两个配置文件,配置的操作中,我的数据库写得是Oracle的,所以大家如果是mysql的,需要注意一下,源码地址:https://git.oschina.net/yellowcong/seasar 我可能以后还会添加一些关于s2dao的类容,官网中文的http://s2container.seasar.org/2.4/zh/DIContainer.html 大家可以看一下,关于容器注入的
目录结构
SeaSar环境搭建
这个环境还是挺坑的,官方的例子都会有坑让你跳,上一个,我们配置了通过app.dicon文件好用,但是到了注解,它还需要导入transaction 和ognl的jar包,而且依赖seasardao包下面是完整,而且需要配置app.dicon和j2ee.dicon两个配置文件,才能做自动注入的操作,一般直接复制粘贴,然后改一下注入的方式和数据库,就可以用了
pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>seasar</groupId>
<artifactId>demo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>demo Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>aliyunmaven</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
<repository>
<id>seasar</id>
<url>https://www.seasar.org/maven/maven2/</url>
</repository>
<repository>
<id>ojdbc6</id>
<url>http://www.datanucleus.org/downloads/maven2/</url>
</repository>
</repositories>
<dependencies>
<!-- 导入jsp -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2.1-b03</version>
<scope>provided</scope>
</dependency>
<!-- 导入servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- seasar框架 的容器 BEGIN-->
<dependency>
<groupId>org.seasar.container</groupId>
<artifactId>s2-framework</artifactId>
<version>2.3.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/jboss/javassist -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.21.0-GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ognl/ognl -->
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>2.6.9-patch-20070624</version>
</dependency>
<!-- Jta需要加入,不然就会报java.lang.NoClassDefFoundError: javax/transaction/TransactionManager -->
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- seasar框架 的容器 END-->
<!--seasar框架 Dao-->
<!-- 不同版本的s2-dao依赖的s2-framework不一样 -->
<dependency>
<groupId>org.seasar.dao</groupId>
<artifactId>s2-dao</artifactId>
<version>1.0.50</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seasar.container/s2-extension -->
<dependency>
<groupId>org.seasar.container</groupId>
<artifactId>s2-extension</artifactId>
<version>2.3.23</version>
</dependency>
<!-- https://mvnrepository.com/artifact/oracle/ojdbc6 -->
<dependency>
<groupId>oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
</dependencies>
<build>
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- 配置war包的名称 -->
<warName>hello</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.6.v20170531</version>
<configuration>
<scanIntervalSecond>10</scanIntervalSecond>
<webApp>
<contextPath>/users</contextPath>
</webApp>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>8080</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
app.dicon
app.icon配置中,我们必须导入dao.dicon的配置,才可以做自动注入的操作,这个dao.dicon在s2-dao-1.0.50.jar包下面默认配置的
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container//EN"
"http://www.seasar.org/dtd/components.dtd">
<components >
<!--dao.dicon 我们必须配置这个dao.dicon,而且还必须有j2ee这个dao文件,这个是s2-dao-1.0.50.jar包下面默认配置的,我草,真尼玛是坑啊-->
<include path="dao.dicon"/>
<!-- ==================================================================== -->
<!-- Component Auto Register -->
<!-- ==================================================================== -->
<!-- Component Auto Register -->
<component class="org.seasar.framework.container.autoregister.FileSystemComponentAutoRegister">
<!-- DAO -->
<initMethod name="addClassPattern" >
<arg>"com.yellowcong.dao"</arg>
<arg>".*DaoImpl"</arg>
</initMethod>
<!-- Business Logic -->
<initMethod name="addClassPattern" >
<arg>"com.yellowcong.service"</arg>
<arg>".*Impl"</arg>
</initMethod>
</component>
</components>
dao.dicon中的配置,这个是默认配置,我们不需要做任何的操作,但是你会看到j2ee.dicon,你就会明白,还必须得配置这个文件,这个文件没有默认的,是我们自己建,
j2ee.dicon
dao.dicon中写了,需要依赖于这个配置文件,所以如果需要写注入,配置的操作中,我的数据库写得是Oracle的,所以大家如果是mysql的,需要注意一下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE components PUBLIC "-//SEASAR//DTD S2Container//EN"
"http://www.seasar.org/dtd/components.dtd">
<components namespace="j2ee">
<component name="transactionManager"
class="org.seasar.extension.jta.TransactionManagerImpl"/>
<component name="requiredTx"
class="org.seasar.extension.tx.RequiredInterceptor"/>
<component name="requiresNewTx"
class="org.seasar.extension.tx.RequiresNewInterceptor"/>
<component name="mandatoryTx"
class="org.seasar.extension.tx.MandatoryInterceptor"/>
<component name="notSupportedTx"
class="org.seasar.extension.tx.NotSupportedInterceptor"/>
<component class="org.seasar.extension.jdbc.impl.BasicResultSetFactory"/>
<component class="org.seasar.extension.jdbc.impl.BasicStatementFactory"/>
<component name="xaDataSource"
class="org.seasar.extension.dbcp.impl.XADataSourceImpl">
<property name="driverClassName">
"oracle.jdbc.OracleDriver"
</property>
<!-- 数据库配置-->
<property name="URL">"jdbc:oracle:thin:@10.0.7.170:1522:ORCL5"</property><property name="user">"SOFINAM_HENSEI"</property><property name="password">"SOFINAM_HENSEI"</property>
</component>
<component name="connectionPool"
class="org.seasar.extension.dbcp.impl.ConnectionPoolImpl">
<property name="timeout">600</property>
<property name="maxPoolSize">10</property>
<property name="allowLocalTx">true</property>
<destroyMethod name="close"/>
</component>
<component name="dataSource"
class="org.seasar.extension.dbcp.impl.DataSourceImpl"/>
</components>
自动注入案例
我这个案例是,通过Seasar2来注入UserDao和UserService的操作,我在上面app.dicon配置文件中配置了注入的方式,在下个博客中,我会详细讲这些注入方式的
UserDao
package com.yellowcong.dao;
/**
*
*作者:yellowcong
*日期:2017/08/29
*時間:16:16:38
*描述:用户操作的Dao
*/
public interface UserDao {
/**
* 获取用户名称
* @param username
* @return
*/
String getUserName(String username);
}
UserDaoImpl
package com.yellowcong.dao.impl;
import com.yellowcong.dao.UserDao;
package com.yellowcong.dao.impl;
import com.yellowcong.dao.UserDao;
/**
*
*作者:yellowcong
*日期:2017/08/29
*時間:16:17:32
*描述:
*/
public class UserDaoImpl implements UserDao{
@Override
public String getUserName(String username) {
return "dao——>"+username;
}
}
UserService
package com.yellowcong.service;
/**
*
*作者:yellowcong
*日期:2017/08/29
*時間:16:48:46
*描述:
*/
public interface UserService {
/**
* 获取用户名称
* @param username
* @return
*/
String getUserName(String username);
}
UserServiceImpl
package com.yellowcong.service.impl;
import com.yellowcong.dao.UserDao;
import com.yellowcong.service.UserService;
/**
*
*作者:yellowcong
*日期:2017/08/29
*時間:16:49:32
*描述:
*/
public class UserServiceImpl implements UserService{
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public String getUserName(String username) {
return "serviceDao——>"+ this.userDao.getUserName(username);
}
}
测试
基于配置文件测试类
这个测试方法是通过代码获取到容器,然后获取容器中UserService的组件,再调用他的方法
package com.yellowcong.test;
import org.seasar.framework.container.S2Container;
import org.seasar.framework.container.factory.S2ContainerFactory;
import com.yellowcong.dao.UserDao;
import com.yellowcong.service.UserService;
/**
*
* 作者:yellowcong 日期:2017/08/29 time:15:58:24 描述:
*/
public class Demo {
//配置文件的位置
private static final String APP_PATH = "app.dicon";
public static void main(String[] args) {
//获取到容器对象
S2Container container = S2ContainerFactory.create(APP_PATH);
//容器初始化
container.init();
UserService userService = (UserService) container.getComponent(UserService.class);
String str = userService.getUserName("yellowcong");
System.out.println(str);
}
}
基于S2DaoTestCase测试
seasar2提供了一个测试类,这个测试类是S2DaoTestCase ,测试方法继承它,然后注入我们需要调用的类,然后在setUp中,配置我们的配置文件路径,然后就可以想Spring的junit测试方法一样操作了,不需要复杂的自己实例化容器了
package com.yellowcong.test;
import org.junit.Test;
import org.seasar.dao.unit.S2DaoTestCase;
import com.yellowcong.service.UserService;
/**
*
*作者:yellowcong
*日期:2017/08/30
*時間:8:51:02
*描述:
*/
public class Demo2 extends S2DaoTestCase{
private UserService userService ;
public void setUserService(UserService userService) {
this.userService = userService;
}
protected void setUp() throws Exception {
super.setUp();
include("app.dicon");
}
@Test
public void test() {
String str = userService.getUserName("junit test");
System.out.println(str);
}
}
测试结果
测试结果成功,我们的包自动注入到了Bean中