新手求助,各位,我在搭建一个spring,mybatis,mySQL的jar应用环境。但是遇见了接口无法自动注入的问题,求助!
1,jar应用的main函数
public class Main {
public static void main(String[] args) {
//这里加载sping环境
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"ApplicationContext.xml");
//这里就是调用数据库
ItemService itemService = new ItemService();
List<Item> list = new ArrayList<Item>();
list = itemService.getItemListWithWebnameAndChanneltitle("1","1");
System.out.println(list.size());
}
2,spring配置
?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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.zdsc.rssagent"/>
<!-- 环境相关配置、功能开关配置请作为property配置-->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:RSSAgent.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
<!-- 链接数据库 -->
<bean id="cyfxbDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${cyfxb.url}" />
<property name="username" value="${cyfxb.user}" />
<property name="password" value="${cyfxb.password}" />
<!-- data source configuration -->
<property name="initialSize" value="60" /><!-- initial connections -->
<property name="maxActive" value="100" /><!-- MAX connections -->
<property name="maxIdle" value="50" /><!-- MAX idle connections -->
<property name="minIdle" value="10" /><!-- MIN idle connections -->
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="validationQuery" value="select 1" />
<property name="timeBetweenEvictionRunsMillis" value="20000" />
<property name="numTestsPerEvictionRun" value="100" />
</bean>
<!-- MyBatis sqlSessionFactory -->
<bean id="cyfxbSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="cyfxbDataSource" />
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<property name="mapperLocations" value="classpath:mapper/cyfxb/*.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" autowire="byName">
<property name="basePackage" value="com.zdsc.rssagent.repo.cyfxb" />
<property name="sqlSessionFactoryBeanName" value="cyfxbSqlSessionFactory" />
</bean>
<bean id="cyfxbTransactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="cyfxbDataSource" />
</bean>
<tx:annotation-driven />
<aop:aspectj-autoproxy />
</beans>
3,SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="defaultExecutorType" value="REUSE"/>
<setting name="defaultStatementTimeout" value="25000"/>
</settings>
</configuration>
4,mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zdsc.rssagent.repo.cyfxb.ItemRepo">
<resultMap id="itemResultMap" type="com.zdsc.rssagent.dto.Item">
<constructor>
<idArg javaType="int" column="itemid"/>
<arg javaType="String" column="webname" />
<arg javaType="String" column="channeltitle" />
<arg javaType="String" column="itemtitle" />
<arg javaType="String" column="itemlink" />
<arg javaType="String" column="description" />
<arg javaType="String" column="category" />
<arg javaType="String" column="pubDate" />
</constructor>
</resultMap>
<select id="getItemListWithWebnameAndChanneltitle" parameterType="String" resultMap="itemResultMap">
select * from item where webname=#{0}AND channeltitle=#{1};
</select>
</mapper>
5,ItemRepo.class
@Repository("itemRepo")
public interface ItemRepo {
public List<Item> getItemListWithWebnameAndChanneltitle(String webName,String channelTitle);
}
6,ItemService.class
@Service
public class ItemService {
@Resource(name="itemRepo")
private ItemRepo itemRepo;
public List<Item> getItemListWithWebnameAndChanneltitle(String webName,String channelTitle){
List<Item> list = new ArrayList<Item>();
list = itemRepo.getItemListWithWebnameAndChanneltitle(webName,channelTitle);
return list;
}
}
7,我的jar包
aopalliance.jar
aspectjweaver-1.6.jar
c3p0-0.9.1.jar
cglib-nodep-2.2.2.jar
commons-beanutils.jar
commons-codec.jar
commons-collections-3.2.1.jar
commons-dbcp-1.4.jar
commons-digester.jar
commons-fileupload-1.2.jar
commons-httpclient-3.0.jar
commons-io-2.4.jar
commons-lang-2.6.jar
commons-logging-1.1.3.jar
commons-pool-1.4.jar
fastjson-1.1.26.jar
fluent-hc-4.3.1.jar
guava-16.0.1.jar
httpclient-4.3.6.jar
httpclient-cache-4.3.1.jar
httpcore-4.3.jar
httpcore-nio-4.3-beta2.jar
httpmime-4.3.6.jar
javassist-3.16.1-GA.jar
jdom-1.1.jar
joda-time-2.4.jar
jstl-1.2.jar
junit-4.8.2.jar
log4j-1.2.15.jar
mybatis-3.1.1.jar
mybatis-spring-1.1.1.jar
mysql-connector-java-5.1.21.jar
rome-1.0.jar
servlet-api.jar
slf4j-api-2.0.99.jar
slf4j-log4j12-1.6.2.jar
spring-aop-4.0.5.RELEASE.jar
spring-aspects-4.0.5.RELEASE.jar
spring-beans-4.0.5.RELEASE.jar
spring-context-4.0.5.RELEASE.jar
spring-context-support-4.0.5.RELEASE.jar
spring-core-4.0.5.RELEASE.jar
spring-expression-4.0.5.RELEASE.jar
spring-jdbc-4.0.5.RELEASE.jar
spring-orm-4.0.5.RELEASE.jar
spring-tx-4.0.5.RELEASE.jar
spring-web-4.0.5.RELEASE.jar
spring-webmvc-4.0.5.RELEASE.jar
当运行的结果:
[10/25 02:25:25] [INFO] ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c1b77f8: startup date [Sun Oct 25 02:25:25 CST 2015]; root of context hierarchy
[10/25 02:25:25] [INFO] XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [ApplicationContext.xml]
[10/25 02:25:26] [INFO] PropertyPlaceholderConfigurer: Loading properties file from class path resource [RSSAgent.properties]
Exception in thread "main" java.lang.NullPointerException
at com.zdsc.rssagent.service.cyfxb.ItemService.getItemListWithWebnameAndChanneltitle(ItemService.java:24)
at com.zdsc.rssagent.Main.main(Main.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
我debug的时候发现的问题是在
@Service
public class ItemService {
@Resource(name="itemRepo")
private ItemRepo itemRepo;
public List<Item> getItemListWithWebnameAndChanneltitle(String webName,String channelTitle){
List<Item> list = new ArrayList<Item>();
list = itemRepo.getItemListWithWebnameAndChanneltitle(webName,channelTitle);
return list;
}
}
这里itemRepo是null,说明没有自动注入进来。
再次请问各位大虾,我的配置哪里有错误,或者我的使用方式是不是有误,我知道spring和mybatis是还有其他方式可以结合的,但是为什么别人可以使用的自动扫描接口的方式,我为什么不能,所以我还是希望能够时候配置出来,恳请各位帮忙了。
你的ItemRepo的实现呢?代码里给出的是interface。
@Reporitory加到ItemRepo的实现上面。
其他代码没看,只看了【1,jar应用的main函数】就有问题。
ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");
ItemService itemService = new ItemService();
ItemService 你居然用的是new!!!要是让你new出来还用Spring干嘛。也就是整个项目中没有用到Spring。你用Spring就是为了让Spring帮你new而不是自己new。
这样才对:
ItemService itemService = ctx.getBean(ItemService.class);