I create these classes:

Quoter:

package quotes; public interface Quoter { void sayQuote(); } 

TerminatorQuoter:

 package quotes; public class TerminatorQuoter implements Quoter { private String message; public void setMessage(String message) { this.message = message; } @Override public void sayQuote() { System.out.println("message = " + message); } } 

Main:

  package quotes; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("context.xml"); context.getBean(TerminatorQuoter.class); } } 

context.xml is located in the resources folder and has the following content:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="quotes.TerminatorQuoter" id="terminatorQuoter"> <property name="message" value="I'll be back"/> </bean> </beans> 

The following is displayed on the console:

 окт 19, 2016 9:05:28 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2e5d6d97: startup date [Wed Oct 19 21:05:28 MSK 2016]; root of context hierarchy окт 19, 2016 9:05:28 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [context.xml] 

Why was my message not printed?

    1 answer 1

    Because you need to call the sayQuote() method:

     Quoter q = context.getBean(TerminatorQuoter.class); q.sayQuote(); 
    • And also, it seems to me, it would be Quoter q = context.getBean("terminatorQuoter"); correct Quoter q = context.getBean("terminatorQuoter"); - Igor Kudryashov