Several ways to inject spring
Understanding the association between classes
One, set injection
Before this we have Spring jar package, can go to the official website, add to WebContent under Web-inf Lib
This approach has to implement the Set method first, now take the Hero (Hero Interface) and weapons (weapon) as an example to explain the first step, first we create two interfaces
Hero interface
~~~
Package com.inter;
Public interface Hero {
void Fight ();
}
武器接口
Package com.inter;
Public interface Weapon {
void attack ();
}
####第二步,然后我们建立两个实现类吕布(Lvbu)实现Hero接口,方天画戟(FangTianHuaJi)实现Weapon
Package com.test;
Import Com.inter.Hero;
Import Com.inter.Weapon;
public class Lvbu implements hero{
Weapon weapon;public Weapon getWeapon() { return weapon;}//shipublic void setWeapon(Weapon weapon) { this.weapon = weapon;}@Overridepublic void fight() { System.out.println("吕布拿起武器"); weapon.attack();}
}
Package com.test;
Import Com.inter.Weapon;
public class Fangtianhuaji implements weapon {
@Overridepublic void attack() { // TODO Auto-generated method stub System.out.println("方天画戟卡卡砍");}
}
再来一个武器青龙偃月刀
Package com.test;
Import Com.inter.Weapon;
public class Qing implements weapon{
@Overridepublic void attack() { // TODO Auto-generated method stub System.out.println("青龙偃月杀杀");}
}
###第三步,我们得创建一个配置文件spring.xml
####第四步,建立一个测试main
Package com.test;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Com.inter.Hero;
Import Com.inter.Performer;
public class Test {
public static void main(String[] args) { // TODO Auto-generated method stub //获取配置文件 ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml"); //得到英雄类 Hero per=(Hero)context.getBean("lvbu"); per.fight(); }
}
####结果###二,自动装配autowire####byName,当我们的吕布有两个属性的时候,也就是说他有两把武器的时候,我们还是这样写,就会报错。将FangTianHuaJi类的id去了一个和lvbu类的属性weapon一样的名字,并且在lvbu bean中添加了autowire="byName"用于指明装配类型是byName自动装配。这个时候lvbu bean就是在上下文中找名为weapon的bean装配到他自己的weapon属性中。####byType,这里已经不用关注FangTianHuaJi类对应的bean的id是什么了,因为已经定义lvbu bean的autowire属性为"byType"。这个时候lvbu bean会在上下文中寻找和weapon具有相同类型的类对应的bean。这里两把刀是相同的属性,所以还这样写就会报错一种方法是将其中一个bean的primary属性设为false,比如:将青龙偃月Qing bean的primary属性设为true,那么吕布的武器就变成青龙偃月刀了###三,使用注解 @Autowired 自动装配####通过基于注解的方式,可以不用在xml文件中为guanyu bean添加autowire属性了,但是注意加启动注解标签spring.xml
~~~
And adding @autowired to the weapon properties of BU, indicating that the annotation is automatically injected
package com.test;import org.springframework.beans.factory.annotation.Autowired;import com.inter.Hero;import com.inter.Weapon;public class Lvbu implements Hero{ //需要在属性头上添加注解 @Autowired Weapon weapon; public Weapon getWeapon() { return weapon; } //set注入,时必须写 public void setWeapon(Weapon weapon) { this.weapon = weapon; } @Override public void fight() { System.out.println("吕布拿起武器"); weapon.attack(); }}
Notice here we are like above, we have two weapons of the same type, we still have to make a distinction, add @qualifier annotations
XML additions
~~~
~~~
The BU weapons Here are also added @qualifier annotations
package com.test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import com.inter.Hero;import com.inter.Weapon;public class Lvbu implements Hero{ //需要在属性头上添加注解 @Autowired //要和配置的对应 @Qualifier("weaponoflvbu") Weapon weapon; public Weapon getWeapon() { return weapon; } //set注入,时必须写 public void setWeapon(Weapon weapon) { this.weapon = weapon; } @Override public void fight() { System.out.println("吕布拿起武器"); weapon.attack(); }}
can also be simpler to write, directly in @qalifier (); Riga ID
<!-- 当我们使用注解时需要启动注解 --> <context:annotation-config /> <bean id="lvbu" class="com.test.Lvbu" ></bean> <bean id="fang" class="com.test.FangTianHuaJi" ></bean> <bean id="qing" class="com.test.Qing"></bean>
Iv. Automatic detection of spring
The Context:component-scan element scans the specified package and all child packages, and finds classes that can be automatically registered as spring beans. The Base-package property identifies the package scanned by the Context:component-scan element.
To automatically detect label beans
By default, Context:component-scan finds classes that are annotated with stereotype (stereotype) annotations, which are the following special annotations:
Type description
@component generic stereotype annotations that indicate that the class is a spring component.
The @Controller identity defines the class as a spring MVC Controller.
The @Repository identity defines the class as a data warehouse (for example, the DAO layer).
The @Service identity defines the class as a service (for example, services layer).
Here we are ordinary writing, so we use @component, in fact, these annotations can be used, but we feel the other inappropriate
XML configuration
~~~
~~~
package com.test;import org.springframework.stereotype.Component;import com.inter.Weapon;//属于吕布的武器@Component("weaponOflvbu")public class FangTianHuaJi implements Weapon { @Override public void attack() { // TODO Auto-generated method stub System.out.println("方天画戟卡卡砍"); }}
package com.test;import org.springframework.stereotype.Component;import com.inter.Weapon;//属于关羽 的武器@Component("weaponOfguanyu")public class Qing implements Weapon{ @Override public void attack() { // TODO Auto-generated method stub System.out.println("青龙偃月杀杀"); } }
package com.test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import com.inter.Hero;import com.inter.Weapon;public class Lvbu implements Hero{ //需要在属性头上添加注解 @Autowired //要和配置的对应,如果一个接口只对应一个实现类的话就都不用写qualifier @Qualifier("weaponOflvbu") Weapon weapon; public Weapon getWeapon() { return weapon; } //set注入,时必须写 public void setWeapon(Weapon weapon) { this.weapon = weapon; } @Override public void fight() { System.out.println("吕布拿起武器"); weapon.attack(); }}
If an interface only corresponds to an implementation class, you do not have to write qualifier, generally others write the project is an interface corresponding to an implementation class, so generally do not see qualifier
Basic usage of Spring