简体   繁体   中英

How to inject in the general class

I hava a project with springmvc

my services folder
在此处输入图片说明

my spring servlet-context.xml

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

Other services are working well

@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();
   }

}

this is a websocket ServerEndpoint

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

I want to inject AdminService to SystemServerEndPoint,so I tried

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

and

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

but serverEndPointService is null
How can I inject success

now i tried like this

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

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

it's ok,serverEndPointService() is not null,but newsMapper in ServerEndPointService is 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>

Put these lines:

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

In your root-context.xml instead of your servlet-context.xml

See if that works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM