Index
1. Xml File Content
2. Usage of Bean
3. Bean Class
Project structure is:
SpringBeanExample
--src/main/java
--com.example.stringexample.xml.hello
* App.java
* HelloBean.java
--src/main/resources
*spring-config.xml
1. Xml File Content( spring-confix.xml )
This file defines bean.
2. Usage of Bean
Create a context object using xml file. Then get bean and call bean method.
3. Bean Class
1. Xml File Content
2. Usage of Bean
3. Bean Class
Project structure is:
--com.example.stringexample.xml.hello
* App.java
* HelloBean.java
*spring-config.xml
This file defines bean.
<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-2.5.xsd"> <bean id="helloBean2" class="com.example.springexample.xml.hello.HelloBean" /> </beans>
Create a context object using xml file. Then get bean and call bean method.
package com.example.springexample.xml.hello; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context =
new ClassPathXmlApplicationContext("spring-config.xml"); HelloBean bean = (HelloBean) context.getBean("helloBean2"); bean.setValue("bean value"); bean.printValue(); } }
package com.example.springexample.xml.hello; public class HelloBean { private String value; public void setValue(String value) { this.value = value; } public void printValue(){ System.out.println("value: " + value); } }