1、曾经xml的配置
<bean id="accountService" class="com.spring.service.Impl.IAccountServiceImpl"
scope="" init-method="" destroy-method="">
<property name="" value="" / ref=""></property>
</bean>
pox.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>
<packaging>jar</packaging>
<groupId>com.spring</groupId>
<artifactId>SpringIocIn</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
2、注解配置
分类:
- 用于创建对象
他们的作用就和在xml配置文件中编写一个<bean>标签的实现功能是一样的。 - 用于注入数据
他们的作用就和在xml配置文件中的<bean>标签中写一个<property>标签的作用是一样的。 - 用于改变作用范围
他们的作用集合在bean标签中使用scope属性实现的功能是一样的。 - 和生命周期有关
他们的作用就和在bean标签中使用init-method和destory-method是一样的
3、用于创建对象
- Component:
作用:用于把当前的类对象存入Spring容器中。
属性:value用于指定bean的id。当我们不写的时候,它的默认值是当前的类名,且首字母小写。
bean.xml文件 这里需要用到context这个名称空间
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 告知spring在创建容器的时候要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为context名称空间和约束中。-->
<context:component-scan base-package="com.spring"></context:component-scan>
</beans>
类 这里采用用accountService作为id
package com.spring.service.Impl;
import com.spring.dao.IAccountDao;
import com.spring.dao.Impl.IAccountDaoImpl;
import com.spring.domain.Account;
import com.spring.service.IAccountService;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component(value = "accountService")
public class IAccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void saveAccount() {
System.out.println("保存");
}
}
测试类 当扫描xml文件的时候就自动给创建了类对象
package com.spring.ul;
import com.spring.domain.Account;
import com.spring.service.IAccountService;
import com.spring.service.Impl.IAccountServiceImpl;
import javafx.application.Application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 表现层
*/
public class Client {
public static void main(String[] args) {
//获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//根据id获取bean对象
// IAccountServiceImpl accountService = (IAccountServiceImpl) ac.getBean("accountService");
IAccountService accountService = ac. getBean("accountService",IAccountService.class);
accountService.saveAccount();
}
}
- Controller:一般用于表现层
- Service:一般用在业务层
- Repository:一般用在持久层
- 以上三个注解他们的作用和属性与Component是一摸一样。
- 他们三个是spring框架为我们提供明确的三层使用的注解,使我们对三层对象更加清晰的。
4、用于注入数据
- Autowired:
作用:自动按类型注入,只要容器有唯一的bean对象类型和要注入的变量类型匹配,就可以成功注入。
如果ioc容器中没有任何bean类型和要注入的变量类型匹配,则报错。
如果ioc容器中有多个类型匹配时,那么它首先找到对应接口的实现类,然后根据变量名=IOC容器中的id去寻找答案
出现的位置:可以是变量上,也可以是方法上。
细节:在使用注解注入时,set方法就不是必须的了
package com.spring.service.Impl;
import com.spring.dao.IAccountDao;
import com.spring.dao.Impl.IAccountDaoImpl;
import com.spring.domain.Account;
import com.spring.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service("accountService")
public class IAccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
public void saveAccount() {
accountDao.saveAccount();
}
}
如果ioc容器中有多个类型匹配时,且对应的变量名也不匹配对应容器中的值时,@Autowire解决不了
- @Qualifier
作用:在按照类中注入的基础上再按照名称注入。它给类成员注入时,不能单独使用,再给方法参数注入时可以。
属性:value用于指定注入bean的id
package com.spring.service.Impl;
import com.spring.dao.IAccountDao;
import com.spring.dao.Impl.IAccountDaoImpl;
import com.spring.domain.Account;
import com.spring.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.Date;
@Service("accountService")
public class IAccountServiceImpl implements IAccountService {
@Autowired
@Qualifier("accountDao")
private IAccountDao accountDao;
public void saveAccount() {
accountDao.saveAccount();
}
}
- Resource
作用:直接按照在bean标签中使用的id注入。他可以独立使用
属性:name:用于指定bean的id
package com.spring.service.Impl;
import com.spring.dao.IAccountDao;
import com.spring.dao.Impl.IAccountDaoImpl;
import com.spring.domain.Account;
import com.spring.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.annotation.Resources;
import java.util.Date;
@Service("accountService")
public class IAccountServiceImpl implements IAccountService {
@Resource(name="accountDao")
private IAccountDao accountDao;
public void saveAccount() {
accountDao.saveAccount();
}
}
以上三个注解只能注入其他bean类型的数据,而基本类型的String类型无法使用上述注解实现。另外集合类型的注入只能通过XML来实现。
- Value:用于注入基本类型和String类型的数据
属性:value用于指定数据的值。他可以使用spring中的SpEl(Spring的EL表达式)
SpEl的写法:${表达式}
package com.spring.dao.Impl;
import com.spring.dao.IAccountDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
@Repository("accountDao")
public class IAccountDaoImpl implements IAccountDao {
@Value(value="1")
private int id;
public void saveAccount() {
System.out.println("保存" + id);
}
}
5、用于改变作用范围
他们的作用就和在bean标签中使用scope属性实现的功能是一样的
- Scope
作用:用于指定bean的作用范围
属性:value 指定范围的值。常取值:singleton、prototype
默认是singleton
package com.spring.service.Impl;
import com.spring.dao.IAccountDao;
import com.spring.dao.Impl.IAccountDaoImpl;
import com.spring.domain.Account;
import com.spring.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.annotation.Resources;
import java.util.Date;
@Service("accountService")
@Scope("prototype")
public class IAccountServiceImpl implements IAccountService {
@Resource(name="accountDao")
private IAccountDao accountDao;
public void saveAccount() {
accountDao.saveAccount();
}
}