1.SpringIOC的本质
Spring ioc指的是控制反转,IOC容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。交由Spring容器统一进行管理,从而实现松耦合.(解耦)
控制反转(ioc)他是一种设计思想
来源于狂神说--------------------->狂神说java,见解很细,很到位,
个人这个图对ioc的解释很到位,容易理解
2.Spring 入门
1.创建一个Maven项目
这里我就不在细说了。参考我之前的博客 环境搭建
2.配置依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
3.编写代码
1.准备测试的实体类
public class User {
private String username;
private String password;
}
2.配置spring的xml文件
命名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-4.1.xsd">
<!--bean就是一个java对象,由Spring创建和管理-->
<bean id="user" class="com.lgw.pojo.User">
<!--set注入-->
<property name="username" value="root"/>
<property name="password" value="123"/>
<!--构造器注入-->
<!-- <constructor-arg name="username" value="2"></constructor-arg>-->
<!-- <constructor-arg name="password" value="1"></constructor-arg>-->
</bean>
</beans
-
bean相当于set方法创建对象
public void setUse(User user) { this.user = user; }
-
依赖注入(Dependency Injection,DI)
- property 注入
- constructor-arg 注入
3.junit测试
@Test
public void IocTest2(){
/*获取Spring配置文件*/
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("applicationContext.xml");
/*1.反射获取*/
User bean = applicationContext.getBean(User.class);
/*2.bean--id获取*/
User bean1 = (User) applicationContext.getBean("user");
System.out.println(bean+"-----"+bean.hashCode());
System.out.println(bean1+"-----"+bean1.hashCode());
}
- ClassPathXmlApplicationContext来获取applicationContext.xml配置文件
- 在使用getBean()来获取对象(1.反射获取,2 bean id获取)
- getBean()默认创建对象为单例
3.DI注入
1.准备一个实体类
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> map;
private Set<String> set;
private String wife;
private Properties info;
}
2.xml配置文件实现Dl注入
<?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
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--p标签 p:address="12" property
c:address="" 构造器 -->
<bean class="com.lgw.pojo.Address" id="address" p:address="12" >
</bean>
<!--autowire自动装配 -->
<bean class="com.lgw.pojo.Student" id="student" >
<!-- 普通的set注入-->
<property name="name" value="普通的set注入"/>
<!--引用bean注入 ref-->
<property name="address" ref="address"/>
<!--数组-->
<property name="books" >
<array value-type="java.lang.String">
<value>book1</value>
<value>book2</value>
</array>
</property>
<!--list集合-->
<property name="hobbys">
<list>
<value>hobby1</value>
<value>hobby2</value>
</list>
</property>
<!-- map集合-->
<property name="map" >
<map>
<entry key="map" value="Spring"/>
<entry key="map1" value="SpringIOC"/>
</map>
</property>
<!-- set集合-->
<property name="set">
<set>
<value>COC</value>
</set>
</property>
<!-- null-->
<property name="wife">
<null></null>
</property>
<!--propreties-->
<property name="info">
<props>
<prop key="name">root</prop>
</props>
</property>
</bean>
</beans>
4.Spring的注解
1.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解-->
<context:annotation-config/>
<!-- com.*或者com..*都可以 扫描com包下面的 -->
<context:component-scan base-package="com.*"></context:component-scan>
</beans>
2.注解的使用
注解说明-
- @Service用于Service层
- @repository 用于dao层-
- @controller用于action层
- @component其他类
本质上没有区别。只是为了区分
@Component
@Scope(value = "singleton")
public class People {
@Autowired
@Qualifier(value = "knife")
private arms a;
private String name;
@Override
public String toString() {
return "People{" +
"a=" + a +
", name='" + name + '\'' +
'}';
}
}
- @Autowired(required = false) 通过byType寻找
- @Qualifier(value="gun") //选择哪个Value值为对应的值
- @Nullable 注解后,字段可以为null
- @Scope 作用域
- @value 赋值(简单赋值)
3.junit测试
@Test
public void Test1(){
ApplicationContext ApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
People bean = ApplicationContext.getBean(People.class);
System.out.println(bean);
}
运行结果:
People{a=knife{type='刀'}, name='null'}
注解开发先对于xml开发要方便快速的很多。
4.纯注解Spring
- 完全使用注解开发
1.java配置文件
@Configuration跟applicationContext.xml配置拥有同等效益
@bean相当于xml的bean标签
@Configuration
@ComponentScan("com.*")//扫描 默认扫描
public class appConfig {
@Bean
public User user(){
return new User();
}
@Bean
public People people(){
return new People();
}
}
5.总结
- 在xml配置适用于数据库配置
- 注解配置 用于不初始值配置
- @Bean 用于初始值配置
来源:oschina
链接:https://my.oschina.net/u/4365870/blog/4307053