spring学习总结(一)_Ioc基础(下)

时间:2022-09-18 20:50:50

本篇文章继续上篇文章讲解Ioc基础,这篇文章主要介绍零配置实现ioc,现在相信大家项目中也基本都是没有了xml配置文件。废话不多说。一起学习

代码示例

  • BookDao.java
package com.kevin.spring.demo1.dao;

/**
* 图书数据访问接口
*/
public interface BookDao { /**
* 添加图书
*
* @param bookName
* @return
*/
String addBook(String bookName);
}
  • BookDaoImpl.java
/**
* 接口实现类
*/
@Repository
public class BookDaoImpl implements BookDao {
/**
* 添加图书接口
*
* @param bookName
* @return
*/
public String addBook(String bookName) {
return "添加图书《" + bookName + "》成功";
}
}
  • BookService.java
@Service
public class BookService {
@Resource
BookDaoImpl bookDao; public void storeBook(String bookName){
String result = bookDao.addBook(bookName);
System.out.println(result);
} }
  • User.java
package com.kevin.spring.demo1.entity;

import org.springframework.stereotype.Component;

@Component
public class User { public User() {
System.out.println("创建User对象");
} public User(String msg) {
System.out.println("创建User对象" + msg);
} public void show() {
System.out.println("一个学生对象!");
}
}
  • ApplicationConfig.java
package com.kevin.spring.demo1.config;

import com.kevin.spring.demo1.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan(basePackages = "com.kevin.spring.demo1")
public class ApplicationConfig { @Bean
public User getUser(){
return new User("成功");
}
}

测试类

    @org.junit.Test
public void testBook(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class);
BookService bookService = ctx.getBean(BookService.class);
bookService.storeBook("周杰伦");
User user = ctx.getBean("getUser",User.class);
user.show();
}

运行结果

创建User对象
创建User对象成功
添加图书《周杰伦》成功
一个学生对象!

@Configuration

@Configuration 相当于配置文件中的<beans/>

@ComponentScan

@ComponentScan相当于配置文件中的context:component-scan

@Bean

@Bean相当于<bean/>,只能注解在方法和注解上,一般在方法上使用

方法名相当于id

这里注意上面获取user时用的是getUser,因为方法名相当于id

@Value获取properties配置

  • 新建一个dev.properties文件
boy.name = kevin
boy.age = 20
  • BoyUtil.java
package com.kevin.spring.demo1.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; @Component
@PropertySource("classpath:dev.properties")
public class BoyUtil { @Value("${boy.name}")
private String name;
@Value("${boy.age}")
private String age; @Override
public String toString() {
return "BoyUtil{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
//get和set方法省略
}

测试

    @org.junit.Test
public void testBoyUtil(){
ApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class);
BoyUtil boyUtil = ctx.getBean(BoyUtil.class);
System.out.println(boyUtil);
}

输出结果

创建User对象
创建User对象成功
BoyUtil{name='kevin', age='20'}

好了,springIoc基础就暂时告一段落。玩的开心

代码:https://github.com/runzhenghengbin/spring-study

参考:https://www.cnblogs.com/best/p/5727935.html#_label3