这里写目录标题
我对Spring的理解
我理解的Spring相当于一个大容器,存储用户需要的对象。两个核心思想IOC和AOP
IOC意思是控制反转,我的理解是在容器中放了各种各样的对象,这种思想的好处就是用户需要哪个对象,直接拿过来就能组装上。传统的思想是对容器中的对象按照需求进行更改,然后再组装。
IOC的思想极大的方便了程序员的开发,只关注业务层,找到自己需要的组件就可以了。不需要根据用户需求频繁的修改组件
AOP就是切片编程,可以在程序中加塞,做某种操作,比如说添加日志之类的功能
依赖注入DI
我对依赖注入的理解就是给容器中的各个对象注入属性值的过程
xml注入
- 编写数据类型,pojo对象
public class Address {
private String address;
//省略get和set方法
}
package com.syw.setdependcy.pojo;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
// 省略get和set方法
public void show(){
System.out.println("name="+ name
+ ",address="+ address.getAddress()
+ ",books="
);
for (String book:books){
System.out.print("<<"+book+">>\t");
}
System.out.println("\n爱好:"+hobbys);
System.out.println("card:"+card);
System.out.println("games:"+games);
System.out.println("wife:"+wife);
System.out.println("info:"+info);
}
}
- 新建配置文件,名字任意,一般命名为applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.syw.setdependcy.pojo.Address">
<property name="address" value="河南"/>
</bean>
<bean id="student" class="com.syw.setdependcy.pojo.Student">
<property name="name" value="宋老师"/>
<property name="address" ref="address"/>
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<property name="games">
<set>
<value>lol</value>
<value>wow</value>
<value>cf</value>
</set>
</property>
<property name="hobbys">
<list>
<value>看电影</value>
<value>打游戏</value>
</list>
</property>
<property name="card">
<map>
<entry key="phone" value="17737148713"/>
<entry key="age" value="30"/>
</map>
</property>
<property name="info">
<props>
<prop key="username">root</prop>
<prop key="password">111111</prop>
</props>
</property>
<property name="wife">
<null/>
</property>
</bean>
</beans>
- 如何获取Spring容器中的对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = context.getBean("student", Student.class);
student.show();
自动注入
首先创建各种对象
public class Cat {
public void shout(){
System.out.println("miao~");
}
}
public class Dog {
public void shout(){
System.out.println("wang~");
}
}
public class People {
private String name;
private Dog dog;
private Cat cat;
}
1.使用注解注入
配置applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--使用注解第一种方式:添加注解驱动-->
<context:annotation-config/>
<!-- 配置bean对象 -->
<bean id="cat" class="com.syw.autowired.pojo.Cat"/>
<bean id="dog" class="com.syw.autowired.pojo.Dog"/>
<bean id="people" name="people2" class="com.syw.autowired.pojo.People">
<property name="name" value="测试人"/>
</bean>
</beans>
注意添加注解驱动 context:annotation-config/
修改People对象
public class People {
private String name;
@Autowired
@Qualifier(value = "dog")
private Dog dog;
@Resource
//@Resource(name = "cat")
private Cat cat;
}
注意:
1.@Autowired是Spring的注解,可以单独使用,默认byType去容器中匹配,如果同一个类型有多个对象,可以配合@Qualifier指定注入对象。@Qualifier不能单独使用
2.@Resource是java的注解,默认byName,如果不设置name属性则byType查找
2.使用xml配置自动注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--使用注解第一种方式:添加注解驱动-->
<context:annotation-config/>
<!-- 配置bean对象 -->
<bean id="cat" class="com.syw.autowired.pojo.Cat"/>
<bean id="dog" class="com.syw.autowired.pojo.Dog"/>
<bean id="people" name="people2" class="com.syw.autowired.pojo.People" autowire="byName">
<property name="name" value="测试人"/>
</bean>
</beans>
autowire="byName"表示People会引入容器中id(cat)与set方法后边名字(setCat)相等的对象
autowire="byType"表示People会引入容器中和属性 类型相同的对象
获取对象方法
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//可以通过id获取,也可以通过<bean name="people2">中的name获取
People people = context.getBean("people", People.class);
people.getDog().shout();
people.getCat().shout();
推荐使用 @Autowired&&@Qualifier(value = “dog”) || @Resource
配置bean对象方法
通过 扫描+类注解的方法 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 指定扫描com.com.syw.autowired.pojo包下的所有类中的注解
注意:扫描包时,会扫描指定包下的所有子孙包 -->
<!-- 但是必须给bean对象配置注解,让对象可扫描
@Component("cat") //这四个注解完全一样,以下三个只是为了分层
@Service("cat") //Service层
@Controller("cat") //Web层
@Repository("cat") //Dao层 -->
<context:component-scan base-package="com.syw.autowired.pojo"/>
</beans>
通过 xml直接引入 配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--使用注解第一种方式:添加注解驱动-->
<context:annotation-config/>
<!-- 配置bean对象 -->
<bean id="cat" class="com.syw.autowired.pojo.Cat"/>
<bean id="dog" class="com.syw.autowired.pojo.Dog"/>
<bean id="people" name="people2" class="com.syw.autowired.pojo.People" autowire="byName">
<property name="name" value="测试人"/>
</bean>
</beans>
通过javaConfig注入
据说新技术,通过代码配置对象,不需要通过xml配置
- 编写对象
//@Component相当于把对象放到容器中,可扫描到
//如果在JavaConfig.java中使用@Bean的方式,则不需要配置@Component
@Component
public class Person {
@Value("张飞")
private String name;
2.编写配置类,管理对象。作用相当于applicationContext.xml
@Configuration
@ComponentScan(value = "com.syw.javaconfigdi.pojo")
public class JavaConfig {
// java方式配置注入方式一:@Configuration+@Bean
// Person对象不需要配置,@Bean相当于相当于<beans>中嵌套了<bean> singlton模式
// @Bean
// public Person person(){
// return new Person();
// }
// java方式配置注入方式二:@Configuration+@ComponentScan
// 该方式需要在Person添加@Component注解,
// 因为只有添加了@Component注解,才把Person对象放到ioc容器,才能扫描到
public Person getPerson(){
return new Person();
}
}
@Configuration 表明当前类是一个配置类,是bean的容器,可以转为cglib类型,避免出现多例
@ComponentScan(value = “com.syw.javaconfigdi.pojo”) 配置扫描bean对象的位置
- 对象获取
ApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
//getBean的key是pojo的类名小写
Person person = context.getBean("person", Person.class);
System.out.println(person.getName());
本片博客仅作为个人学习记录,难免有理解错误