熟悉Spring 3的同学,来帮帮忙呀
刚开始捣鼓Spring,用Spring MVC,但给controller里装配一个service接口的实现类时,死活装不上,麻烦帮忙看下。
用注解的方式,可以正常装配,就是在controller类的属性前注解@Autowired,在服务实现类前注解@Service,这样是可以的,但用XML方式,却不能装配。
这是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"> <!-- 配置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>
<?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>
<?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>
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; } }
package com.woods.seller.service;public interface ProductService {}
package com.woods.seller.service.impl;import org.springframework.stereotype.Service;import com.woods.seller.service.ProductService;// @Service // 用注解方式时,可以正常装配public class ProductServiceImpl implements ProductService {}
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,该属性将不被设置。