目录













- package com.superdemo.service.impl;
-
- import com.superdemo.service.UserService;
- 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
- public class UserServiceImpl implements UserService {
- public void save(){
- System.out.println("user service running...");
- }
- //设定bean的生命周期
- @PostConstruct
- public void init(){
- System.out.println("user service init");
- }
- //设定bean的生命周期
- @PreDestroy
- public void destroy(){
- System.out.println("user service destroy");
- }
- }

- package com.superdemo;
-
- import com.superdemo.dao.BookDao;
- import com.superdemo.dao.UserDao;
- import com.superdemo.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();
- }
- }

- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druidartifactId>
- <version>1.2.11version>
- dependency>

- package com.superdemo.config;
-
- import com.alibaba.druid.pool.DruidDataSource;
- import org.springframework.context.annotation.Bean;
- import org.springframework.stereotype.Component;
-
- //为能被spring扫描加载到,后面有替代方案,这里简单用Component写下
- @Component
- public class JDBCConfig {
-
- @Bean("dataSource")
- public DruidDataSource getDataSource(){
- DruidDataSource ds = new DruidDataSource();
- ds.setDriverClassName("com.mysql.jdbc.Driver");
- ds.setUrl("jdbc:mysql://localhost:3306/dp1?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8&useSSL=false");
- ds.setName("root");
- ds.setPassword("123456");
- return ds;
- }
- }

- DruidDataSource dataSource = (DruidDataSource) ctx.getBean("dataSource");
- System.out.println(dataSource);

- @Value("3")
- private int num;
- @Value("icpc")
- private String version;




- @Autowired
- private UserDao userDao2;
- @Autowired
- @Qualifier("bookDao2")
- private BookDao bookDao;



- package com.superdemo.service.impl;
-
- import com.superdemo.dao.BookDao;
- import com.superdemo.dao.UserDao;
- import com.superdemo.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.PropertySource;
- 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
- @PropertySource("classpath:jdbc.properties")
- public class UserServiceImpl implements UserService {
-
- @Value("3")
- private int num;
- @Value("icpc")
- private String version;
- @Autowired
- private UserDao userDao2;
- @Autowired
- @Qualifier("bookDao2")
- private BookDao bookDao;
-
- @Value("${jdbc.userName}")
- private String userName;
- @Value("${jdbc.password}")
- private String password;
-
- public void save(){
- System.out.println("user service running..."+num+" "+version);
- System.out.println("properties: "+userName+" "+password);
- userDao2.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.superdemo.config;
-
- import org.springframework.context.annotation.ComponentScan;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- @ComponentScan("com.superdemo")
- public class SpringConfig {
- }

- public class App {
- public static void main(String[] args) {
- //ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
-
- 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);*/
- }
- }












