首页 新闻 会员 周边 捐助

spring在Web中的注入问题

0
悬赏园豆:20 [已解决问题] 解决于 2015-12-06 19:18
我想问一下spring ioc依赖注入的问题,我写了一个测试用例,单机下没有问题,但在web下就不能注入了

下面分别是测试用例,部分源码,和一些情况

@Test
public void test()
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-beans.xml");
UserService userService = (UserService) ctx.getBean("userService");
System.out.println(userService.findByUsername("Asens"));

SampleRealm sr=(SampleRealm)ctx.getBean("sampleRealm");
UserService us=sr.getUserService();
System.out.println(us.findByUsername("Asens"));
System.out.println(us);

}

out:

true
true
cn.asens.WebTest.service.impl.UserServiceImpl@5bc11082



这是我写的一个测试,是可以正常运行的
下面是源码

spring-beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="userService" class="cn.asens.WebTest.service.impl.UserServiceImpl"></bean>

<bean id="sampleRealm" class="cn.asens.WebTest.realm.SampleRealm">
<property name="userService" ref="userService"></property>
</bean>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="false">

<!-- Spring配置文件开始 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-beans.xml,
classpath:spring-shiro-web.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring配置文件结束 -->

<!-- shiro 安全过滤器 -->
<!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.xml -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<!-- Make sure any request you want accessible to Shiro is filtered. /* catches all -->
<!-- requests. Usually this filter mapping is defined first (before all others) to -->
<!-- ensure that Shiro works in subsequent filters in the filter chain: -->
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
 


UserService
public interface UserService {
public boolean findByUsername(String username);
}

UserServiceImpl

public class UserServiceImpl implements UserService {
@Override
public boolean findByUsername(String username) {
if(username.equals("Asens"))
{
return true;
}
return false;
}
}

SampleRealm(有点长就不全贴了)

private UserService userService;

public void setUserService(UserService userService) {
this.userService = userService;
}

public UserService getUserService() {
return userService;
}

但是在web中就不能正常注入了

如果把
private UserService userService;

改成

private UserService userService=new UserServiceImpl();

就可以正常运行,说明程序本身的逻辑没问题
test可以正常运行,证明注入问题也不大
但是不明白为什么在web环境下不行


Asens的主页 Asens | 初学一级 | 园豆:177
提问于:2015-12-06 11:30
< >
分享
最佳答案
0

你WEB中怎么获取的SampleRealm?别自己new

收获园豆:20
之奇一昂 | 小虾三级 |园豆:1421 | 2015-12-06 15:10

已经解决了,realm是shiro框架下的生成的,bean要跨xml注入,多谢,分送你了

Asens | 园豆:177 (初学一级) | 2015-12-06 19:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册