<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.xmm.pojo.Address">
<property name="address" value="西安"/>
</bean>
<bean id="student" class="com.xmm.pojo.Student">
<!-- 第一种普通值注入,values-->
<property name="name" value="卡卡西"/>
<!-- 第2种,Bean注入r,ref-->
<property name="address" ref="address" />
<!-- 数组-->
<property name="books">
<array>
<value>红楼梦</value>
<value>尚书</value>
<value>楚辞</value>
<value>宋词</value>
</array>
</property>
<!-- list-->
<property name="hobbys">
<list>
<value>听歌</value>
<value>写BUG</value>
<value>弹吉他</value>
<value>看电影</value>
</list>
</property>
<!-- Map-->
<property name="card">
<map>
<entry key="身份证" value="123213213123"/>
<entry key="银行卡" value="1232123213123"/>
</map>
</property>
<!-- Set-->
<property name="games">
<set>
<value>LOL</value>
<value>DNF</value>
<value>COC</value>
</set>
</property>
<!-- NULL-->
<property name="wife">
<null/>
</property>
<!-- properties-->
<property name="info">
<props>
<prop key="driver">SQL</prop>
<prop key="url">**</prop>
<prop key="username">root</prop>
<prop key="password">*******</prop>
</props>
</property>
</bean>
</beans>
Student{name='卡卡西'
address=Address{address='西安'}
books=[红楼梦, 尚书, 楚辞, 宋词]
hobbys=[听歌, 写BUG, 弹吉他, 看电影]
card={身份证=123213213123, 银行卡=1232123213123}
games=[LOL, DNF, COC]
info={password=*******
url=**
driver=SQL
username=root}
wife='null'}
- p-namespace 命名空间注入
xmlns:p=“http://www.springframework.org/schema/p”
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- ---P命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.xmm.pojo.User" p:name="鼬" p:age="18"/>
- c-namespace 注入
xmlns:c=“http://www.springframework.org/schema/c”
<?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">
<!-- ---P命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.xmm.pojo.User" p:name="鼬" p:age="18"/>
<bean id="user2" class="com.xmm.pojo.User" c:age="18" c:name="自来也"/>
</beans>
Bean Scopes
1.singleton:默认值。
当IOC容器一创建就会创建bean的实例,而且是单例的,每次得到同一个。
<bean id="user2" class="com.xmm.pojo.User" c:age="18" c:name="自来也" scope="singleton"/>
2.prototype:原型模式。
当IOC容器一创建不再实例化该bean,每次调用getBean方法时再实例化该bean,而且每次调用都会返回一个新的实例
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
以下 web开发中使用
3.request:每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境
4.session:同一个HTTP Session 共享一个Bean,不同的 HTTP Session 使用不同的Bean. 该作用域仅适用于WebApplicationContext环境。
Bean的自动装配 @Autowire
- 自动装配是Spring满足Bean依赖一种方式
- Spring会在上下文中自动寻找,并自动给Bean装配属性
在spring中有三种装配的方式
- 在xml种显示配置
- 在java种显示配置
- 隐式自动装配bean **
byname
需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性set方法的值一致
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.xmm.pojo.Cat"/>
<bean id="dog" class="com.xmm.pojo.Dog"/>
<!--
byname: 会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
-->
<bean id="people" class="com.xmm.pojo.People" autowire="byName">
<property name="name" value="卡卡西"/>
<!-- <property name="dog" ref="dog"/>-->
<!-- <property name="cat" ref="cat"/>-->
</bean>
</beans>
byType
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.xmm.pojo.Cat"/>
<bean class="com.xmm.pojo.Dog"/>
<!--
byname: 会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
byType: 会自动在容器上下文中查找,和自己对象属性类型相同的bean
-->
<bean id="people" class="com.xmm.pojo.People" autowire="byType">
<property name="name" value="卡卡西"/>
<!-- <property name="dog" ref="dog"/>-->
<!-- <property name="cat" ref="cat"/>-->
</bean>
</beans>
需要保证bean的class唯一,并且这个bean需要和自动注入的属性的类型一致
注解实现自动装配
- 导入约束
- 配置注解的支持 ->>> 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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
== @Autowird ==
直接在属性上使用即可,也可以在set方式上使用
使用Autowired我们可以不用编写Set方法,前提是自动装配的属性在IOC(Spring)容器中存在,且符合名字byname
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
@Resource和@Autowiredqub
- 都是用来自动装配的,都可以放在属性字段
- @Autowired通过byType实现
- @Resource默认通过Byname方式实现,如果找不到名字,则通过byType实现
@Autowired:自动装配 通过类型,名字
如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value=“xxx”)
**@Resource:**自动装配通过名字,类型
@Componet:组件 放在类上,说明这个Spring管理了.就是bean
//等价于 <bean id="user" class="com.xmm.dao.User"/>
//@Component 组件
@Component
public class User {
public String name="卡卡西";
}
属性如何注入
//等价于 <bean id="user" class="com.xmm.dao.User"/>
//@Component 组件
@Component
public class User {
public String name;
//相当于 <property name="name" value="自来也">
@Value("自来也")
public void setName(String name){
this.name=name;
}
}
衍生注解
@Component 在web开发中,按照mvc三层架构分层
- dao [@Repository]
- service [@Service]
- controller [@Controller]
四个注解功能都是一样的,都代表将某个类注册到Spring中,装配Bean
作用域
@scope(“singleton”)
xml与注解:
- xml更万能,适用于任何场合! 维护更简单方便
- 注解 维护较复杂
- xml用来管理bean
- 注解只负责完成属性的注入
<!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.xmm.pojo"/>
<context:annotation-config/>
JavaConfig
- 使用java方式配置Spring
@Configuration:就是一一个配置类
@Configuration 也会被Spring容器托管,注册到容器中,因其本来就是一个@Component
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(
annotation = Component.class
)
String value() default "";
boolean proxyBeanMethods() default true;
}
pojo
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("小鸭子")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
}
配置
@Configuration
@ComponentScan("com.xmm.pojo")
public class Myconfig {
/**
* 注册一个Bean 相当于 xml中配置 bean标签
* 方法的名字,相当于bean标签中id属性
* 方法返回值,相当于Bean标签中class属性
*/
@Bean
public User getUser(){
return new User();//返回注入到bean的对象
}
}
Test
public class MyTest {
public static void main(String[] args) {
//若完全使用配置类方式去做,只能通过AnnotationConfig 上下文来获取容器,通过配置类的class对象加载
ApplicationContext context = new AnnotationConfigApplicationContext(Myconfig.class);
User getUser = context.getBean("getUser", User.class);
System.out.println(getUser.getName());
}
}