tags:

views:

139

answers:

3

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

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?

+1  A: 

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

<property name="name" value="my value"/>
predhme
do i not have to use "ref" instead of value if I am referring to another bean in the config file?
bmw0128
that is correct.
predhme
A: 

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)

Tim
i'm not familiar with doing the config with annotations yet, when one does annotations, one does not need to edit the config file, and...is it easy enough to explain @Repository?
bmw0128
hey, tim, another sort of off this topic, i have a page that is just a CRUD page. i figured i'd have a "service" with the profile in it, so the service can call its "save" method and pass in the profile, you are saying that this is not a good idea?
bmw0128
Here's a link to the documentation for the first question, it's better than any attempt on my part to explain it.. ;) http://static.springsource.org/spring/docs/2.5.x/reference/beans.html#beans-annotation-config(But saying something is a Repository defines it as a bean which is most likely some form of Data Access Object)
Tim
As for the second question: A service is typically something that's stateless (anyone correct me if I'm wrong), so a reference to a single StudentProfile (in case you have StudentProfiles for each Student in the system) would break this stateless contract and make it apply only to that particular StudentProfile. Have a look at the Data Access Object pattern and try to work from there to a stateless StudentService bean, and pick it up from there.. (Hint: stateless would mean something like having a method: "public StudentProfile getStudentProfileForStudentName(String studentName);"
Tim
thanks, i appreciate it
bmw0128
No problem, we're all there to teach and learn, and "Newbie Sping Questions" are just within my range of abilities.. ;) (Although admittedly the answer mostly is "Use annotations, they rule!" ;)) On a more serious note: Some people disapprove of using annotations for this, citing Separations of Concerns and other stuff, so you might want to see if this is of any concern to you.. Personally I really like to do away with the XML and see from the code which is what (bean / service / repository / autowired etc), but you'll have to decide for yourself
Tim
+2  A: 

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.

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

[Disclaimer: not tested]

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

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