Two ways of obtaining Bean
One way: get the id
Sample code:
HelloWorld helloWorld=(HelloWorld)ctx.getBean("id名称");
Second way: Get by type
Sample code:
HelloWorld helloWorld=(HelloWorld)ctx.getBean("类型名.class");
Advantages and disadvantages:
Examples acquired by the id is unique , except by injection type in the configuration file configured with only a bean
If you configure more than one of the same type of bean, then get the object will be reported by Second way is not the only bean error
Two ways of getting ways bean must have the default constructor with no arguments
3 ways dependent injected
- Properties injection
- Constructor injection
- Factory injection
First, the injection properties:
By injecting the set method: set methods need to be included in the class: you need to have a configuration method in the class setName
<property name="name" value="spring" ></property>
Second, the construction method requires injecting :( corresponding constructor can), if there is more than a few more rows can be
<constructor-arg value="name"></constructor-arg>
Question 1 : If the presence of two constructors, and the same number of parameters thereof, but a different type , for example, is a is a double int, how to identify IOC container?
Workaround: Add the type attribute indicates the type of the parameter. as follows
<constructor-arg value="name" type="java.lang.String"></constructor-arg>
Question 2 : If the same format, the same type but different parameter order it?
Workaround: Add the index attribute indicates the position of the parameter. as follows
<constructor-arg value="name" index="1"></constructor-arg>
Also to note is that index and type attributes that can be mixed, it can be used simultaneously
Three, injection factory method:
One way: static factory method injection
Second way: The method of injection instance factory
Static factory method : The following is not being given only to actual plant design class should not be so
Note that the method must be factory i.e. substituting the following parameters get method, and specify the path and factory class constructor in the configuration file
import com.taotao.beans.HelloWorld; public class StaticFactory { public static HelloWorld getHelloWorld(String name) { return new HelloWorld(name); } }
injection
<!--class要写工厂的类;factory-method要写工厂类中用于创建bean的方法 --> <bean id="factoryHelloWorld" class="com.taotao.beans.factory.StaticFactory" factory-method="getHelloWorld"> <constructor-arg value="Tom"></constructor-arg> </bean>
Main:
public class Main { public static void main(String[] args) { //获取spring ioc容器(从配置的xml文件中获取) ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); //获取bean HelloWorld helloWorld=(HelloWorld)ctx.getBean("factoryHelloWorld"); //调用hello方法 helloWorld.hello(); } }
HelloWorld class:
public class HelloWorld { private String name; public void setName(String name) { this.name=name; } public void hello() { System.out.println("hello "+name); } public HelloWorld() { super(); } public HelloWorld(String name) { super(); this.name = name; } }
Instance factory method :
And differs from the above that the method just less static, and configuration is different, the following
<bean id="instanceFactory" class="com.taotao.beans.factory.InstanceFactory"></bean> <bean id="instanceHelloWorld" factory-bean="instanceFactory" factory-method="getHelloWorld"> <constructor-arg value="Rose"></constructor-arg> </bean>
InstanceFactory class
import com.taotao.beans.HelloWorld; public class InstanceFactory { public HelloWorld getHelloWorld(String name) { return new HelloWorld(name); } }
Like all other