Spring中为什么我每次getBean()都为空

atong3079 2010-11-18 09:15:06
Spring中为什么我每次getBean()都为空,我明明配置了applicationContext.xml。为什么没给我注入进去啊,
public class SaveUserAction extends ActionSupport {
User user;
UserService userService;

public UserService getUserService() {
return userService;
}

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

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}
public String execute() throws Exception {
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userSerice=(UserService)factory.getBean("userService");
userSerice.add(user);
return SUCCESS;
}

}
...全文
1203 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
atong3079 2010-11-22
  • 打赏
  • 举报
回复
<?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.0.xsd" >


<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/student"></property>
<property name="username" value="root" />
<property name="password" value="chenlin"></property>

</bean>
<bean id="sessionfactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">


<property name="mappingResources">
<list>
<value>com/bean/User.hbm.xml</value>
</list>
</property>

<property name="dataSource" ref="datasource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>

</property>

</bean>
<bean id="userDao" class="com.dao.imp.UserDaoImp">
<property name="sessionFactory">
<ref bean="sessionfactory"/>
</property>
</bean>
<bean id="UserService" class="com.manager.imp.Usermanager">
<property name="userDao" ref="userDao"></property>

</bean>
<bean id="saveUserAction" class="com.action.user.SaveUserAction">
<property name="userService" >
<ref bean="UserService"/>
</property>
</bean>
atong3079 2010-11-22
  • 打赏
  • 举报
回复
为什么我配置了spring还是没给我注入呢。难道一定要写工厂?
yangfeihu 2010-11-19
  • 打赏
  • 举报
回复
难道没访问一次据加载xml构建beanfactory一次吗?

就没有这样写代码的. 可能那个老师是涂个方面给你们写测试例子吧;
重o0o生 2010-11-19
  • 打赏
  • 举报
回复
你可以写一个创建BeanFactory的单例类,,这样就不会次次都得加载applicationContext.xml
haixiang710 2010-11-19
  • 打赏
  • 举报
回复
可以不用工厂去加载的..
在web.xml里配spring

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

启动时spring就会自动在配置的classpath去加载applicationContext.xml
spring就会根据配置文件去自动注入.
aini136234058 2010-11-19
  • 打赏
  • 举报
回复
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
没这样写过

private UserService userService;
直接这样就可以拿到啊。。
atong3079 2010-11-18
  • 打赏
  • 举报
回复
原来大小写搞错了,请问大家,是不是每次都要构建一个beanfactory?
public String execute() throws Exception {
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userSerice=(UserService)factory.getBean("userService");
userService.add(user);
可不可以不构建beanfactory来获得实例直接写
userService.add(user)吗?
我看郎溪老师就是这样写的,我的出空指针异常,他的怎么没问题?
cntaizi 2010-11-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 tanwan 的回复:]

引用 1 楼 atong3079 的回复:
各位大侠们,求解啊。我定义了bean为什么提示No bean named 'userService' is defined

把applicationContext.xml贴出来看看
[/Quote]

+1
tanwan 2010-11-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 atong3079 的回复:]
各位大侠们,求解啊。我定义了bean为什么提示No bean named 'userService' is defined
[/Quote]
把applicationContext.xml贴出来看看
atong3079 2010-11-18
  • 打赏
  • 举报
回复
各位大侠们,求解啊。我定义了bean为什么提示No bean named 'userService' is defined

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧