标签:单例 ota instance singleton ons shc utf-8 oct 简单
Spring ioc指的是控制反转,IOC容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。交由Spring容器统一进行管理,从而实现松耦合.(解耦)
控制反转(ioc)他是一种设计思想
来源于狂神说--------------------->狂神说java,见解很细,很到位,
个人这个图对ioc的解释很到位,容易理解
这里我就不在细说了。参考我之前的博客 环境搭建
<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>
public class User {
private String username;
private String password;
}
命名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)
@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());
}
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;
}
<?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>
<?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>
注解说明-
本质上没有区别。只是为了区分
@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 + ‘\‘‘ +
‘}‘;
}
}
@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开发要方便快速的很多。
@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();
}
}
标签:单例 ota instance singleton ons shc utf-8 oct 简单
原文地址:https://www.cnblogs.com/sky1023/p/13088617.html