springioc应用
一、xml方式
-
新建空白工程
-
新建模块
-
导入依赖
<dependencies>
<!-- ioc 90% 核心功能只此一个就够了-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
</dependencies>
4.新建目录和包名
- 新建xml文件
二、set 注入代码
POM
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<!-- ioc 90%-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
</dependencies>
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.lgx.dao.IndexDaoImpl"></bean>
<bean id="service" class="com.lgx.dao.IndexService">
<property name="indexDao" ref="dao"></property>
</bean>
</beans>
xml p 空间写法
IndexDao
package com.lgx.dao;
/**
* Created by hasee on 2019-11-09.
*/
public interface IndexDao {
public void test();
}
IndexDaoImpl
package com.lgx.dao;
/**
* Created by hasee on 2019-11-09.
*/
public class IndexDaoImpl implements IndexDao {
@Override
public void test() {
System.out.println("impl");
}
}
IndexService
package com.lgx.dao;
/**
* Created by hasee on 2019-11-09.
*/
public class IndexService {
private IndexDao indexDao;
public void service(){
indexDao.test();
}
public void setIndexDao(IndexDao indexDao) {
this.indexDao = indexDao;
}
}
Test
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext
=new ClassPathXmlApplicationContext("classpath:spring.xml");
IndexService service = (IndexService) classPathXmlApplicationContext.getBean("service");
service.service();
}
}
二、构造函数注入代码
IndexService
package com.lgx.dao;
/**
* Created by hasee on 2019-11-09.
*/
public class IndexService {
private IndexDao indexDao;
public IndexService(IndexDao indexDao) {
this.indexDao = indexDao;
}
public void service(){
indexDao.test();
}
}
XML
<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.lgx.dao.IndexDaoImpl"></bean>
<bean id="service" class="com.lgx.dao.IndexService">
<!--<property name="indexDao" ref="dao"></property>-->
<constructor-arg ref="dao"></constructor-arg>
</bean>
</beans>
相对于set 注入p空间写法,这里也有c空间写法,不再赘述
注解方式
@Component("dao")
public class IndexDaoImpl implements IndexDao {
@Override
public void test() {
System.out.println("impl");
}
}
@Service("service")
public class IndexService {
@Autowired
private IndexDao indexDao;
public void service(){
indexDao.test();
}
}
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
>
<context:component-scan base-package="com"></context:component-scan>
</beans>
二、javabase方式
新建一个配制类
import org.springframework.stereotype.Component;
/**
* Created by hasee on 2019-11-09.
*/
@Configuration
@ComponentScan("com.lgx.dao")
//@ImportResource("classpath:spring.xml") 可以混合使用
public class Spring {
}
更改容器新建方式
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContex
=new AnnotationConfigApplicationContext(Spring.class);
IndexService service = (IndexService) annotationConfigApplicationContex.getBean("service");
service.service();
}
}
三、自动装配
- xml形式
自动装配有两种 byName 和byType。
byType 根据类型来。如果两个类实现在同一接口,会报错
byName 取值为下图所示是根据 set 方法名。
- javabase 形式
@autowrired
@Resource
@Service
public class IndexService {
// @Resource(type = IndexDaoImpl1.class)
// @Resource(name = "indexDao")
@Resource
private IndexDao indexDao;
public void service(){
indexDao.test();
}
public void setIndexDaooooooo(IndexDao indexDao) {
this.indexDao = indexDao;
}
}
3. 关于自动扫包beanName的生成规则,参考官网。
四、作用域问题
- 最简单demo
@Service
//@Scope("singleton")
@Scope("prototype")
public class IndexService {
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context=
new AnnotationConfigApplicationContext(Spring.class);
IndexService service = (IndexService) context.getBean("indexService");
System.out.println(service.hashCode());
IndexService service1 = (IndexService) context.getBean("indexService");
System.out.println(service1.hashCode());
}
- service单例 dao多例
方法一:
@Repository
@Scope("prototype")
public class IndexDaoImpl implements IndexDao {
@Override
public void test() {
System.out.println("impl");
}
}
@Service
public class IndexService implements ApplicationContextAware {
private ApplicationContext applicationContext;
public void service(){
IndexDao indexDao=this.applicationContext.getBean("command", IndexDao.class);
indexDao.test();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
方法二:
@Service
public abstract class IndexService {
@Lookup
public abstract IndexDao getIndexDao();
public void service(){
System.out.println(getIndexDao().hashCode());
getIndexDao().test();
}
}
五、spring 的生命周期回调
方式一
@Service
public class IndexService implements InitializingBean,DisposableBean {
@Autowired
private IndexDao indexDao;
public void service(){
indexDao.test();
}
@Override
public void destroy() throws Exception {
System.out.println("destory......");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("init .......");
}
}