<bean id="accountService" class="com.spring.service.Impl.IAccountServiceImpl"
scope="" init-method="" destroy-method="">
property>
bean>
pox.xml
<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.0modelVersion>
<packaging>jarpackaging>
<groupId>com.springgroupId>
<artifactId>SpringIocInartifactId>
<version>1.0-SNAPSHOTversion>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.0.2.RELEASEversion>
dependency>
<dependency>
<groupId>commons-dbutilsgroupId>
<artifactId>commons-dbutilsartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.6version>
dependency>
<dependency>
<groupId>c3p0groupId>
<artifactId>c3p0artifactId>
<version>0.9.1.2version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
project>
分类:
<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">
<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();
}
}
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解决不了
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();
}
}
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来实现。
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);
}
}
他们的作用就和在bean标签中使用scope属性实现的功能是一样的
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();
}
}