如何在Spring启动期间强制实例化所有引用的bean?

时间:2022-01-09 20:47:17

My applicationContext.xml:

<bean id="studentService" class="com.coe.StudentService">
    <property name="studentProfile" ref="studentProfile" />
</bean>

<bean id="studentProfile" class="com.coe.student.StudentProfile">

</bean>

My web.xml:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

My classes:

StudentService{
private StudentProfile studentProfile;
//has appropriate getters/setters

}

StudentProfile{
private String name;
//has getter/setter

}

i have a jsp that calls studentService.studentProfile.name, and the error says that studentProfile is null

我有一个调用studentService.studentProfile.name的jsp,错误说studentProfile为null

My assumption is that when the server starts up, Spring instantiates all objects as requested, so when the StudentService is called, would not Spring also set StudentProfile?

我的假设是,当服务器启动时,Spring会根据请求实例化所有对象,所以当调用StudentService时,Spring也不会设置StudentProfile吗?

3 个解决方案

#1


Not really an answer to your question, but a possible solution to your problem, if you're willing to work with annotations instead:

如果您愿意使用注释来代替问题,那么这不是您问题的答案,而是您问题的可能解决方案:

Web.xml

<!-- Spring -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>spring.xml</param-value>
</context-param>

Spring.xml

<context:component-scan base-package="com.coe" />

Java code

@Service
StudentService{
@Autowired
private StudentProfile studentProfile; }

@Repository//???
StudentProfile{
private String name;}

That said I have a little trouble understanding why StudentProfile would be a bean (assuming every student has a profile) and StudentService would have reference to a single StudentProfile, but that might just be your terminology.. (Or my lack of understanding thereof)

这说我理解为什么StudentProfile会成为一个bean(假设每个学生都有个人资料)并且StudentService会引用一个StudentProfile,但这可能只是你的术语..(或者我对它缺乏了解)

#2


Normally with Spring and a web-app, you would have a DispatcherServlet and a bunch of controllers, that pass onto JSP views. These controllers would be managed by Spring.

通常使用Spring和Web应用程序,您将拥有一个传递到JSP视图的DispatcherServlet和一堆控制器。这些控制器将由Spring管理。

If you want to go direct to a JSP without using DispatcherServlet, then you need some way to make the first injection into your pages (ContextLoaderListener doesn't do this). That is, you must explicitly lookup the initial bean using JSP initialization code such as

如果你想在不使用DispatcherServlet的情况下直接进入JSP,那么你需要一些方法来第一次注入你的页面(ContextLoaderListener不会这样做)。也就是说,您必须使用诸如的JSP初始化代码显式查找初始bean

[Disclaimer: not tested]

[免责声明:未经测试]

<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%!
    private StudentService studentService;

    public void jspInit() {
        studentService = (StudentService) WebApplicationContextUtils.
            getRequiredWebApplicationContext(getServletContext()).
                getBean("studentService");                  
    }
%>

#3


perhaps your name property is the null item. try setting a value

也许你的名字属性是空项目。尝试设置一个值

<property name="name" value="my value"/>

#1


Not really an answer to your question, but a possible solution to your problem, if you're willing to work with annotations instead:

如果您愿意使用注释来代替问题,那么这不是您问题的答案,而是您问题的可能解决方案:

Web.xml

<!-- Spring -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>spring.xml</param-value>
</context-param>

Spring.xml

<context:component-scan base-package="com.coe" />

Java code

@Service
StudentService{
@Autowired
private StudentProfile studentProfile; }

@Repository//???
StudentProfile{
private String name;}

That said I have a little trouble understanding why StudentProfile would be a bean (assuming every student has a profile) and StudentService would have reference to a single StudentProfile, but that might just be your terminology.. (Or my lack of understanding thereof)

这说我理解为什么StudentProfile会成为一个bean(假设每个学生都有个人资料)并且StudentService会引用一个StudentProfile,但这可能只是你的术语..(或者我对它缺乏了解)

#2


Normally with Spring and a web-app, you would have a DispatcherServlet and a bunch of controllers, that pass onto JSP views. These controllers would be managed by Spring.

通常使用Spring和Web应用程序,您将拥有一个传递到JSP视图的DispatcherServlet和一堆控制器。这些控制器将由Spring管理。

If you want to go direct to a JSP without using DispatcherServlet, then you need some way to make the first injection into your pages (ContextLoaderListener doesn't do this). That is, you must explicitly lookup the initial bean using JSP initialization code such as

如果你想在不使用DispatcherServlet的情况下直接进入JSP,那么你需要一些方法来第一次注入你的页面(ContextLoaderListener不会这样做)。也就是说,您必须使用诸如的JSP初始化代码显式查找初始bean

[Disclaimer: not tested]

[免责声明:未经测试]

<%@ page import="org.springframework.web.context.support.WebApplicationContextUtils" %>
<%!
    private StudentService studentService;

    public void jspInit() {
        studentService = (StudentService) WebApplicationContextUtils.
            getRequiredWebApplicationContext(getServletContext()).
                getBean("studentService");                  
    }
%>

#3


perhaps your name property is the null item. try setting a value

也许你的名字属性是空项目。尝试设置一个值

<property name="name" value="my value"/>