如何注入普通班

时间:2014-08-15 04:20:13

标签: java spring spring-mvc websocket inject

我使用springmvc开展了一个项目

我的服务文件夹
enter image description here

我的spring servlet-context.xml

<annotation-driven />
<context:component-scan base-package="com.ldl.origami.origamiService" />

其他服务运作良好

@Service
public class ServerEndPointService {

   @Autowired(required = false)
   private NewsMapper newsMapper;


   public int getUnReadNews(String userName) {
        NewsExample example = new NewsExample();
        example.createCriteria().andUsernameEqualTo(userName).andStateEqualTo(0);
        List<News> news = newsMapper.selectByExample(example);
        return news.size();
   }

}

这是一个websocket ServerEndpoint

@ServerEndpoint(value = "/serverEndPoint/{userName}")
public class SystemServerEndPoint

我想将AdminService注入SystemServerEndPoint,所以我尝试了

@ServerEndpoint(value = "/serverEndPoint/{userName}")
public class SystemServerEndPoint{
   @Inject
   private ServerEndPointService serverEndPointService;

@Component
@ServerEndpoint(value = "/serverEndPoint/{userName}")
public class SystemServerEndPoint{
   @Autowired
   private ServerEndPointService serverEndPointService;

但是serverEndPointService为空 我怎样才能注入成功

现在我尝试了这个

@Configuration
@ServerEndpoint(value = "/serverEndPoint/{userName}")
public class SystemServerEndPoint{

    @Bean
    public ServerEndPointService serverEndPointService(){
        return new ServerEndPointService();
    }

没关系,serverEndPointService()不为null,但ServerEndPointService中的newsMapper为null

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-     class>
</listener>

<filter>
   <filter-name>encodingFilter</filter-name>
   <filter-class>
       org.springframework.web.filter.CharacterEncodingFilter
   </filter-class>
   <init-param>
       <param-name>encoding</param-name>
       <param-value>UTF-8</param-value>
   </init-param>
   <init-param>
       <param-name>forceEncoding</param-name>
       <param-value>true</param-value>
   </init-param>
</filter>
<filter-mapping>
   <filter-name>encodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>DruidStatView</servlet-name>
    <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>DruidStatView</servlet-name>
    <url-pattern>/druid/*</url-pattern>
</servlet-mapping>


</web-app>

1 个答案:

答案 0 :(得分:0)

把这些行:

<annotation-driven />
<context:component-scan base-package="com.ldl.origami.origamiService" />

在root-context.xml中,而不是servlet-context.xml

看看是否有效。