spring编码风格
schemal-based-------xml
annotation-based-----annotation
java-based----java Configuration
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lixiang</groupId>
<artifactId>spring-ioc</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
1、set方法注入
//spring.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="dao" class="com.lixiang.DaoImpl"></bean>
<bean id="service" class="com.lixiang.SpringImpl">
<property name="dao" ref="dao"/>
</bean>
</beans>
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
public class SpringImpl {
private Dao dao;
// spring的set方法注入
public void setDao(Dao dao) {
this.dao = dao;
}
public void test(){
dao.test();
}
}
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
public interface Dao {
void test();
}
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
public class DaoImpl implements Dao {
@Override
public void test() {
System.out.println("Dao");
}
}
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
public class TestDemo {
@Test
public void fun1(){
ApplicationContext ca = new ClassPathXmlApplicationContext("classpath:spring.xml");
SpringImpl springImpl = (SpringImpl) ca.getBean("service");
springImpl.test();
}
}
2、构造方法
<?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="dao" class="com.lixiang.DaoImpl"></bean>
<bean id="service" class="com.lixiang.SpringImpl">
<constructor-arg name="dao" ref="dao"/>
</bean>
</beans>
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
public class SpringImpl {
private Dao dao;
// 构造方法
public SpringImpl(Dao dao){
this.dao = dao;
}
public void test(){
dao.test();
}
}
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
public class TestDemo {
@Test
public void fun1(){
ApplicationContext ca = new ClassPathXmlApplicationContext("classpath:spring.xml");
SpringImpl springImpl = (SpringImpl) ca.getBean("service");
springImpl.test();
}
}
以上,需要自己在spring.xml里描述类的依赖关系,spring可以开启自动依赖
可以在spring.xml文件里开启自动依赖,此时不需要在<bean>标签里写<property name="dao" ref="dao"/>和<constructor-arg name="dao" ref="dao"/>此时只需要注入<bean>即可,依赖关系恶意省略.自动注入方式有byType,byName,no和constructor
Mode | Explanation |
---|---|
| (Default) No autowiring. Bean references must be defined by |
| Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name and it contains a |
| Lets a property be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use |
| Analogous to |
<?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"
default-autowire="byName">
<bean id="dao" class="com.lixiang.DaoImpl"/>
<bean id="dao1" class="com.lixiang.DaoImpl1"/>
<bean id="service" class="com.lixiang.SpringImpl"/>
</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: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:component-scan base-package="com"/>
</beans>
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
@Component("service")
public class SpringImpl {
@Autowired
private Dao dao1;
public void test(){
dao1.test();
}
}
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
@Component("dao1")
public class DaoImpl1 implements Dao {
@Override
public void test() {
System.out.println("Dao1");
}
}
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
public class TestDemo {
@Test
public void fun1(){
ApplicationContext ca = new ClassPathXmlApplicationContext("classpath:spring.xml");
SpringImpl springImpl = (SpringImpl) ca.getBean("service");
springImpl.test();
}
}
Java配置类注入
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
@Configuration
@ComponentScan("com")
public class SpringConfig {
}
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
public class TestDemo {
@Test
public void fun1(){
ApplicationContext ca = new AnnotationConfigApplicationContext(SpringConfig.class);
SpringImpl springImpl = (SpringImpl) ca.getBean("service");
springImpl.test();
}
}
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
@Component("service")
public class SpringImpl {
@Autowired
private Dao dao;
public void test() {
dao.test();
}
}
/**
* description
*
* @version 1.0
* @date 2019/8/14
*/
@Component
public class DaoImpl1 implements Dao {
@Override
public void test() {
System.out.println("Dao1");
}
}
总结:
@Autowired和@Resource的区别
当@Autowired根据byType自动依赖查找不到bean时,会根据byName的方式,去寻找跟属性名一致的bean,此时一个接口可以有多个实现类,但是每个实现类的名称要不一样,如果依赖的属性名跟实现类的名称不一致则会报错
@Resource则反之