熟悉Spring 3的同学,来帮帮忙呀
刚开始捣鼓Spring,用Spring MVC,但给controller里装配一个service接口的实现类时,死活装不上,麻烦帮忙看下。
用注解的方式,可以正常装配,就是在controller类的属性前注解@Autowired,在服务实现类前注解@Service,这样是可以的,但用XML方式,却不能装配。
这是web.xml文件:
- XML code
<?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"> <!-- 配置Spring配置文件的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 使用ContextLoaderListener初始化Spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 定义Web应用的首页 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> </web-app>
这是spring配置文件applicationContext.xml
- XML code
<?xml version="1.0" encoding="GBK"?><!-- 指定Spring配置文件的Schema信息 --><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <bean id="productService" class="com.woods.seller.service.impl.ProductServiceImpl"> </bean> <bean id="productController" class="com.woods.seller.controller.ProductController"> <property name="productSvc" ref="productService"/> </bean></beans>
这是Spring MVC的配置文件:
- XML code
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <!-- 注解支持 --> <mvc:annotation-driven/> <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 --> <context:component-scan base-package="com.woods.seller"> </context:component-scan> </beans>
这是Controller类:
- Java code
package com.woods.seller.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.woods.seller.service.ProductService;@Controllerpublic class ProductController { // @Autowired //用注解方式时,可以正常装配 private ProductService productSvc; public ProductService getProductSvc() { return productSvc; } public void setProductSvc(ProductService productSvc) { this.productSvc = productSvc; } @RequestMapping(value = "listProduct.html") public int listProct() { int a = 1; return a; } }
这是service接口:
- Java code
package com.woods.seller.service;public interface ProductService {}
这是接口的实现类:
- Java code
package com.woods.seller.service.impl;import org.springframework.stereotype.Service;import com.woods.seller.service.ProductService;// @Service // 用注解方式时,可以正常装配public class ProductServiceImpl implements ProductService {}
我用debug,在controller的 int a = 1;这里设断点,用注解方式时,可以看到productSvc已经有值了。但用XML方式,这里是null
请问是啥原因啊?是哪里配置错了?
多谢啊
[解决办法]
<bean id="productService" class="com.woods.seller.service.impl.ProductServiceImpl">
</bean>
这个类里有没有使用注解? 比如注入了dao,
如果没有,应该是没问题的
[解决办法]
你可以试试把注解功能关闭了
<!-- 注解支持 -->
<mvc:annotation-driven/>
<!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
<context:component-scan base-package="com.woods.seller">
</context:component-scan>
这段话注释掉,再试试看!
我之前看过有人就是使用了注解和XML配置,导致BEAN无法注入
[解决办法]
用xml的话 要注入的属性名必须和bean中的id名一样
所以
// @Autowired //用注解方式时,可以正常装配
private ProductService productSvc;
改为:
// @Autowired //用注解方式时,可以正常装配
private ProductService productService;
重新生产get set方法
[解决办法]
哦 我估计是这样的
lz的controller 是通过 注解 标示的
@Controller
public class ProductController {}
所以 在springmvc中@controller是没有被注入任何属性的
而你在applicationContext.xml中配置的bean productController
<bean id="productController" class="com.woods.seller.controller.ProductController">
这个类是没有实现Controller接口的,所以spring认为他是个普通的bean,而不是mvc中的controller
所以 服务启动后总共产生了两个com.woods.seller.controller.ProductController类
一个是 通过@controller注解参数的controller类(没有被注入任何属性)
一个是 applicationContext.xml中定义的productController的普通bean(被注入了productSvc对象)
当请求到达时,springmvc会调用通过@Controller注解的类 所以 里面的 productSvc为null
了解了吧
[解决办法]
可以给特定的属性添加@Autowired注解,Spring将会对它进行自动装配。例如,可以给prefixGenerator属性的setter方法加上@Autowired注解。此时,Spring将会试着装配类型与PrefixGenerator兼容的Bean。
Java代码
package com.apress.springrecipes.sequence;
import org.springframework.beans.factory.annotation.Autowired;
public class SequenceGenerator{
...
@Autowired
public void setPrefixGenerator(PrefixGenerator prefixGenerator){
this.prefixGenerator = prefixGenerator;
}
}
如果在IOC容器里存在一个类型与PrefixGenerator兼容的Bean,它将会被自动设置给prefixGenerator属性。
Xml代码
<beans ...>
...
<bean id="sequenceGenerator" class="com.apress.springrecipes.
sequence.SequenceGenerator">
<property name="initial" value="100000"/>
<property name="suffix" value="A"/>
</bean>
<bean id="datePrefixGenerator" class="com.apress.springrecipes.sequence.
DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd"/>
</bean>
</beans>
默认情况下,所有应用了@Autowired注解的属性都需要被设置。当Spring找不到匹配的Bean装配属性时, 将会抛出异常。如果某一属性并不一定需要被设置,那么可以设置@Autowired的required属性为false.此时,如果Spring不能找到匹配的Bean,该属性将不被设置。