我的action总是注入service失败!LoginAction.java:@Controller("loginAction")
//@Component("loginAction")
@Scope("prototype")
//@ParentPackage("struts-default")
@Namespace("/login")
@Results({
@Result(name="success",value="/index.jsp"),
@Result(name="failure",value="/error.jsp")
})
public class LoginAction extends ActionSupport {
//方式一
@Autowired
private UserService userService; //方式二
@Autowired
@Qualifier("userService")
private UserService userService;

//方式三
@Resource
private UserService userService; private String username;
private String password; //@Action(value="/login",results={@Result(name="success",location="/index.jsp")})

public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String checkLogin() throws Exception {
if(null == userService){
return "failure";
};
return "success";
//null){//
} public UserService getUserService() {
return userService;
}

//方式四
@Autowired
public void setUserService(@Qualifier("userService")UserService userService) {
this.userService = userService;
}UserServiceImpl.java:@Service
public class UserServiceImpl implements UserService { @Autowired
private UserDao userDao;

public User addUserService(User user) throws Exception {
// TODO Auto-generated method stub
return null;
}
}struts.xml:<struts>
<!--指定字符集--> 
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.objectFactory.spring.autoWire.alwaysRespect" value="true" /></struts>applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-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/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byType">
    
   <bean id="userService" class="com.jnhy.stsystem.service.impl.UserServiceImpl">
    </bean>
    
    <bean id="loginAction" class="com.jnhy.stsystem.web.action.LoginAction" scope="prototype">
     <property name="userService">
     <ref local="userService"/>
     </property>
    </bean>
     <!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 该 BeanPostProcessor 将自动起作用,对标注的 @Resource、@PostConstruct 以及 @PreDestroy
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>-->
    
<bean id="propertyBean"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/data-source.properties</value>
</property>
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

<!-- 使用注解形式配置 -->
<context:annotation-config/>
<context:component-scan base-package="com.jnhy.stsystem"/>

<bean id="mySqlDataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${stsystem.driverName}">
</property>
<property name="url"
value="${stsystem.jdbcUrl}">
</property>
<property name="username" value="${stsystem.user}"></property>
<property name="password" value="${stsystem.password}"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="mySqlDataSource" />
</property>
<!-- <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration">
</property> -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/jnhy/stsystem/entity/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 基于hibernate的事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 采用注解形式的声明式事务管理 -->
<tx:annotation-driven transaction-manager="txManager"/>

<aop:aspectj-autoproxy/>
<bean id="controllerAspect"
class="com.jnhy.stsystem.aspectj.ControllorAspectj"/>
</beans>请高手给看看!谢啦!

解决方案 »

  1.   

    报什么错?
    action注入有问题么?改成@Controller("\loginAction")试试
      

  2.   

    晕,SSH2使用注解方式实现有这么复杂吗?我怎么配置的没有这么复杂啊!!
      

  3.   

    你的 配置好像有问题哦,既然用的是注解,就不应该在在applicationContex.xml中陪dao和service的东西,用注解 你applicationContex.xml中用注解 少了  声明用注解扫描全部package内容。
    用下面这个类似语句声明用注解扫描全部package内容。<context:component-scan base-package="com.ztev.fipmis" />
    <context:component-scan base-package="com.ztev.webapp" />然后在然后才能用@compont,@controll等等进行操作
      

  4.   

    貌似struts2的注解复杂化了 很多地方不是很推荐使用 
      

  5.   

    楼主既然用到了注解,那么在application的配置文件里应该很简单
    dao与service层可以采用自动扫描
    即可
    <context:component-scan base-package="com.ztev.webapp,com.ztev.fipmis" />
    那么楼主只需在dao中注解@Repository() ,在service中注入@Service 在action中注入@Controller,其他的工具类就可以采用@Component注解。
    如果在service要用到dao我们只需要提供get,set方法即可。