目录
1、基于注解的Ioc配置
<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">
导入
2、基于xml和注解的springioc的操作
1)基于xml:
Client:
package zhujie;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//表现层
public class Client {
//获取Spring的核心容器,并根据id获取对象
public static void main(String[] args) throws ClassNotFoundException {
//1.获取Spring的核心容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean2.xml");
//2.根据id获取对象
IAccountService as = (IAccountService) ac.getBean("accountService");
as.save();
}
}
AccountServiceImpl:
package zhujie;
//业务层实现类
public class AccountServiceImpl implements IAccountService{
private IAccountDao ad = null;
public void setAd(IAccountDao ad) {
this.ad = ad;
}
@Override
public void save() {
ad.save();
}
}
bean.xml
<!-- 配置业务层 -->
<bean id="accountService" class="zhujie.AccountServiceImpl">
<property name="ad" ref="accountDao"></property>
</bean>
<!-- 配置持久层 -->
<bean id="accountDao" class="zhujie.AccountDaoImpl"></bean>
2)基于注解
bean.xml
<!-- 告知spring创建容器时要扫描的包 -->
<context:component-scan base-package="zhujie"></context:component-scan>
AccountServiceImpl:(未注入)
package zhujie;
import org.springframework.stereotype.Component;
//业务层实现类
@Component(value = "accountService")
public class AccountServiceImpl implements IAccountService{
private IAccountDao ad = null;
public void setAd(IAccountDao ad) {
this.ad = ad;
}
@Override
public void save() {
ad.save();
}
}
3、注解的分类
1)用于创建对象:
@Component:
作用:相当于在spring的xml配置文件中写一个bean标签
属性:value:用于指定bean的id。如果不写,默认为当前类名,首字母小写 例:accountServiceImpl
由此衍生的 3个注解:
@Controller:一般用于表现层
@Service:一般用于业务层
@Repository:一般用于持久层
他们的作用和属性和@Component一致
2)用于注入数据:
@Autowired
自动按照类型注入。只要容器中有唯一的类型匹配,则可以直接注入成功
细节:当使用此注解时,set可以省略不写
属性:request:是否必须注入成功,true(默认)/false
package zhujie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
//业务层实现类
@Component(value = "accountService")
public class AccountServiceImpl implements IAccountService{
@Autowired
private IAccountDao accountDao;
public void setAd(IAccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void save() {
accountDao.save();
}
}
package zhujie;
import org.springframework.stereotype.Component;
@Component(value = "accountDao")
public class AccountDaoImpl implements IAccountDao{
@Override
public void save() {
System.out.println("保存账户......");
}
}
当有多例对象时:
package zhujie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
//业务层实现类
@Component(value = "accountService")
public class AccountServiceImpl implements IAccountService{
@Autowired
private IAccountDao accountDao2;
public void setAd(IAccountDao accountDao) {
this.accountDao2 = accountDao;
}
@Override
public void save() {
accountDao2.save();
}
}
@Qualifier:在自动按照类型注入的基础上,再按照beand的id注入。在给类成员注入时,不能独立使用
属性:value指定bean的id
@Resource:直接按照bean的id注入,在同时使用@Autowired和@Qualifier时可以用@Resource代替
:https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api/1.3.2
以上3个注解,都只能用于注入其他bean类型,而不能注入基本类型和String类型
package zhujie;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
//业务层实现类
@Component(value = "accountService")
public class AccountServiceImpl implements IAccountService{
// @Autowired
// @Qualifier("accountDao2")
@Resource(name = "accountDao2")
private IAccountDao accountDao;
public void setAd(IAccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void save() {
accountDao.save();
}
}
@Value :用于注入基本类型和String类型的数据
属性:value:用于指定注入的数据,它支持使用spring的el表达式:${ }
jdbcConfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mydql://localhost:3306/spring
jdbc.username=root
jdbc.password=123456
bean.xml
<context:property-placeholder location="jdbcConfig.properties"/>
package zhujie;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//业务层实现类
@Component(value = "accountService")
public class AccountServiceImpl implements IAccountService{
// @Autowired
// @Qualifier("accountDao2")
@Resource(name = "accountDao2")
private IAccountDao accountDao;
@Value("${jdbc.driver}")
private String driver;
public void setAd(IAccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void save() {
System.out.println(driver);
accountDao.save();
}
}
3)用于改变作用范围:
@Scope:用于改变作用范围,value与xml中的scope一样
4)和生命周期相关:(了解)
@PostContruct:用于指定初始化方法,与配置文件中init-method属性是一样的
@PreDestroy:用于指定销毁方法,与destroy-method一样