Tag: Amp XML File Port string Word GRE file ring method
How beans are configured: Through the full class name (reflection), through the factory method (static factory method & Instance Factory method), Factorybean
This is where the bean is configured based on the full class name
<bean id= "Helloword" class= "Com.spring.HelloWord" >
<property name= "UserName" value= "Springsss" ></property>
</bean>
Dependency Injection Mode:
Attribute injection:
The Applicationcontext.xml configuration file is:
<?xml version= "1.0" encoding= "UTF-8"?
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:p= "http://www.springframework.org/schema/p"
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 "
<!--config bean-->
<bean id=" Helloword " Class= "Com.spring.HelloWord";
<property name= "UserName" value= " Springsss "></PROPERTY>
</bean>
</beans>
Package com.spring;
public class Helloword {
Private String UserName;
public void Setusername (String userName) {
This.username = UserName;
}
public void Hello () {
System.out.println ("Hello:" + userName);
}
Public Helloword () {
System.out.println ("construct!!!!!!!!!!");
}
}
Package com.spring;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void Main (string[] args) {
/**
* Create objects and assign values to spring complete
*/
// Helloword Helloword = new Helloword ();
// Helloword.setusername ("Hello");
// Helloword.hello ();
1. Create a spring IOC container
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
2 getting the bean from the container
Helloword Helloword = (Helloword) ctx.getbean ("Helloword");
3. Calling methods
Helloword.hello ();
}
}
The output is:
Construct!!!!!!!!!!
Hello:springsss
Constructor injection:
Applicationcontext.xml file is
<?xml version= "1.0" encoding= "UTF-8"?
>
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:p= "http://www.springframework.org/schema/p"
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 ">
<!--configuration Bean--
<bean id= "Car" class= "Com.spring.Car" >
<constructor-arg value= "Green" ></constructor-arg>
<constructor-arg value= "></constructor-arg>"
</bean>
</beans>
public class Car {
private String name;
private String color;
private int num;
Public Car (String color, int num) {
Super ();
This.color = color;
This.num = num;
}
@Override
Public String toString () {
Return "Car [name=" + name + ", color=" + Color + ", num=" + num + "]";
}
}
Package com.spring;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void Main (string[] args) {
1. Create a spring IOC container
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml");
Car car = (car) ctx.getbean ("Car");
System.out.println (Car.tostring ());
}
}
The output is:
Car [Name=null, Color=green, num=22]
Spring Configuration bean methods and dependency Injection hair mode