spring基础代码
1、pojo类
- 创建一个Cat类
package pojo; public class Cat { public void show(){ System.out.println("喵喵喵"); } }
- 创建一个Dog类
package pojo; public class Dog { public void show(){ System.out.println("汪汪汪"); } }
- 创建一个User类
package pojo; public class User { private Cat cat; private Dog dog; private String str; public User() { } public User(Cat cat, Dog dog, String str) { this.cat = cat; this.dog = dog; this.str = str; } public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } public String getStr() { return str; } public void setStr(String str) { this.str = str; } @Override public String toString() { return "User{" + "cat=" + cat + ", dog=" + dog + ", str='" + str + '\'' + '}'; } }
2、配置beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dog" class="pojo.Dog"> <!-- collaborators and configuration for this bean go here --> </bean> <bean id="cat" class="pojo.Cat"> <!-- collaborators and configuration for this bean go here --> </bean> <bean id="user" class="pojo.User"> <property name="cat" ref="cat"/> <property name="dog" ref="dog"/> <property name="str" value="zhangsan"/> </bean> </beans>
3、测试
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import pojo.User; public class MyTest { @Test public void testMethod(){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); User user = context.getBean("user", User.class); user.getDog().show(); user.getCat().show(); } }
自动装配
- 自动装配是Spring满足bean依赖的一种方式!
- Spring会在上下文中自动寻找,并自动给bean装配属性
在Spring中有三种装配的方式
- 在xml中显式的配置
- 在java中显式配置
- 隐式的自动装配bean【重要】
byName自动装配
- 我们在bean标签中添加
autowire="byName"
属性,然后就可以通过自己对象set后面的值对象我们bean的id自动装配
<?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 id="cat" class="com.wx.pojo.Cat"/> <bean id="dog" class="com.wx.pojo.Dog"/> <bean id="people" class="com.wx.pojo.People" autowire="byName"> <property name="name" value="wx"/> </bean> </beans>
byType自动装配
- 我们在bean标签中添加
autowire="byType"
属性,会自动在容器上下文查找,和自己对象属性类型相同的bean
<?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 id="cat" class="com.wx.pojo.Cat"/> <bean id="dog" class="com.wx.pojo.Dog"/> <bean id="people" class="com.wx.pojo.People" autowire="byType"> <property name="name" value="wx"/> </bean> </beans>
小结
- 使用ByName的时候,需要保证所有bean的id唯一,并且bean需要和自动配置的属性的set方法的值一致
- 使用ByType的时候,需要保证bean的class唯一,并且这个bean需要和自动注入的属性类型一致
使用注解实现自动装配
jdk1.5支持的注解,Spring2.5支持注解
要使用注解需知:
- 导入约束
xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
- 配置注解的支持
<context:annotation-config/>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置注解的支持--> <context:annotation-config/> <bean id="cat" class="pojo.Cat"/> <bean id="dog" class="pojo.Dog"/> <bean id="user" class="pojo.User"/> </beans>
- @Autowired
直接在属性上使用即可!也可以在set方式上使用!
@Autowired(required=false) 说明: false,对象可以为null;true,对象必须存对象,不能为null。
package pojo; import org.springframework.beans.factory.annotation.Autowired; public class User { @Autowired private Cat cat; @Autowired private Dog dog; private String str; public User() { } public User(Cat cat, Dog dog, String str) { this.cat = cat; this.dog = dog; this.str = str; } public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } public String getStr() { return str; } public void setStr(String str) { this.str = str; } @Override public String toString() { return "User{" + "cat=" + cat + ", dog=" + dog + ", str='" + str + '\'' + '}'; } }
-
@Qualifier
1、@Autowired是根据ByType自动装配的,加上@Qualifier则可以根据ByName的方式自动装配
2、@Qualifier不能单独使用。
<bean id="cat" class="pojo.Cat"/> <bean id="cat1" class="pojo.Cat"/> <bean id="dog" class="pojo.Dog"/> <bean id="dog1" class="pojo.Dog"/> <bean id="user" class="pojo.User"/>
3、没有加入Qualifier注解运行直接报错,加入Qualifier注解
public class User { @Autowired @Qualifier(value = "cat1") private Cat cat; @Autowired @Qualifier(value = "dog1") private Dog dog; private String name; public Cat getCat() { return cat; } public void setCat(Cat cat) { this.cat = cat; } public Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "People{" + "cat=" + cat + ", dog=" + dog + ", name='" + name + '\'' + '}'; } }
- @Resource这个注解在不是spring的,是jdk的,在高版本的jdk已经取消了
1、@Resource如有指定的name属性,先按该属性进行byName方式查找装配;
2、其次再进行默认的byName方式进行装配;
3、如果以上都不成功,则按byType的方式自动装配。
4、都不成功,则报异常。
public class User { @Resource(name = "cat") private Cat cat; @Autowired @Qualifier(value = "dog") private Dog dog; private String name;
小结
-
@Resource与 @Autowired区别:都是用来自动装配的,都可以放在属性字段上
-
@Autowired通过ByType的方式实现
-
@Resource如有指定的name属性,先按该属性进行byName方式查找装配,其次再进行默认的byName方式进行装配 如果以上都不成功,则按byType的方式自动装配。
注解说明
- @Autowired:自动装配通过类型、名字
1、如果您在bean中有相同的类的全限定名, 那么在应用@Autowired时按照逻辑来说就会出现歧义, 这时spring为我们准备了@Qualifier注解来解决这个问题。
2、如果显示的定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
- @Resource:自动装配通过名字、类型
- @Nullable:字段标记了这个注解,说明这个字段可以null
- @Component:组件,放在类上,说明这个类被Spring管理了,就是bean
- @Value:给常量注入一个值
- @Component有几个衍生的注解,我们在web开发中,会按照mvc三层架构分层
a、dao:@Repository
b、service:@Service
c、controller:@Controller
d、这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean - 3、xml与注释
xml更加万能,适用于任何场合,维护简单方便
注解不上是自己的类使用不了,维护相对复杂 - 4、xml与注解最佳实践
xml来管理bean
注解只负责属性的注入
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/94730.html