我正在尝试使用spring hibernate和struts 2。应用程序已成功部署。但是在尝试访问spring bean时我得到NullPointerException。 以下是代码UsersBo是一个类
@Autowired
private UsersBo ubo;
public void setUbo(UsersBo ubo) {
this.ubo = ubo;
}
public UsersBo getUbo() {
return ubo;
}
我查了一下,发现udo是空的。以下是配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="annotatedClasses">
<list>
<value>com.riteshsangwan.ossoc.entities.Users</value>
<value>com.riteshsangwan.ossoc.entities.Files</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
</props>
</property>
</bean>
</beans>
DataSource bean
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ossoc;create=true" />
<property name="username" value="root" />
<property name="password" value="deflection" />
</bean>
</beans>
的applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- Database Configuration -->
<import resource="DataSource.xml"/>
<import resource="HibernateSessionFactory.xml"/>
<!-- Beans Declaration -->
<import resource="UsersBean.xml"/>
</beans>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>ossoc</display-name>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<!-- Filter Start -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- Filter End -->
<!-- Filter Mapping Start -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Filter Mapping End -->
<!-- Listener Start -->
<!-- Spring Start -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
用户bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="loginAction" class="com.riteshsangwan.ossoc.actions.LoginAction">
<property name="ubo" ref="usersBo" />
</bean>
<bean id="usersBo" class="com.riteshsangwan.ossoc.business.UsersBoImpl" >
<property name="udao" ref="usersDAOImpl" />
</bean>
<bean id="usersDAOImpl" class="com.riteshsangwan.ossoc.business.UsersDAOImpl" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
我在课程定义
上面添加了@Repository
注释
答案 0 :(得分:0)
要启用@Autowired
注释,您必须在Spring配置文件(Users bean xml文件)中添加<context:annotation-config />
。您还必须添加上下文命名空间
xmlns:context="http://www.springframework.org/schema/context"
所以,你的xml将是
<?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-2.5.xsd">
<context:annotation-config />
<bean id="loginAction" class="com.riteshsangwan.ossoc.actions.LoginAction">
<property name="ubo" ref="usersBo" />
</bean>
<bean id="usersBo" class="com.riteshsangwan.ossoc.business.UsersBoImpl" >
<property name="udao" ref="usersDAOImpl" />
</bean>
<bean id="usersDAOImpl" class="com.riteshsangwan.ossoc.business.UsersDAOImpl" >
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
希望这有帮助!我还看到setter和getters方法应遵循Java命名约定。因此,spring可以在依赖注入期间找到合适的setter。请更改/遵循代码中的Java命名约定。