applicationContext.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--启动注解驱动,指定对应扫描路径,也就是资源所在的包-->
<context:component-scan base-package="com.itheima"/>
<!-- <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>-->
<!--<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/>
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>-->
</beans>
package com.itheima.service.impl;
import com.itheima.dao.BookDao;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/*定义bean,后面设置bean的id*/
@Component("userService")
/*设置bean的作用域*/
@Scope("singleton")
public class UserServiceImpl implements UserService {
@Value("3")
private int num;
@Value("itheima")
private String version;
@Autowired
private UserDao userDao;
@Autowired
private BookDao bookDao;
/* public void setNum(int num) {
this.num = num;
}
public void setVersion(String version) {
this.version = version;
}*/
public void save(){
System.out.println("user service running......"+num+" "+version);
userDao.save();
bookDao.save();
}
/*设置bean的什么周期*/
@PostConstruct
public void init(){
System.out.println("user service init....");
}
/*设置bean的什么周期*/
@PreDestroy
public void destroy(){
System.out.println("user service destroy.....");
}
}
package com.itheima.service;
public interface UserService {
public void save();
}
import com.alibaba.druid.pool.DruidDataSource;
import com.itheima.dao.BookDao;
import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.save();
/* UserDao userDao = (UserDao) ctx.getBean("userDao");
userDao.save();
BookDao bookDao = (BookDao) ctx.getBean("bookDao");
bookDao.save();*/
/* DruidDataSource dataSource = (DruidDataSource) ctx.getBean("dataSource");
System.out.println(dataSource);*/
}
}
public interface UserDao {
public void save();
}
package com.itheima.dao.impl;
import com.itheima.dao.UserDao;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component("userDao")
public class UserDaoImpl implements UserDao{
public void save(){
System.out.println("user dao running.....");
}
}
package com.itheima.dao.impl;
import com.itheima.dao.UserDao;
import org.springframework.stereotype.Component;
@Component("userDao1")
public class UserDaoImpl1 implements UserDao{
public void save(){
System.out.println("user dao 1 running.....");
}
}
package com.itheima.dao;
public interface BookDao {
public void save();
}
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
@Component
@Primary
public class BookDaoImpl implements BookDao {
public void save(){
System.out.println("book dao running;");
}
}
package com.itheima.dao.impl;
import com.itheima.dao.BookDao;
import org.springframework.stereotype.Component;
@Component
public class BookDaoImpl1 implements BookDao {
public void save(){
System.out.println("book dao 1 running;");
}
}