This article continues to explain the foundation of IOC in the previous article. This article mainly introduces the implementation of IOC with zero configuration. Now I believe that there is no xml configuration file in all projects. Don't talk much nonsense. Learning together
Code example
- BookDao.java
package com.kevin.spring.demo1.dao; /** * Book data access interface */ public interface BookDao { /** * Add books * * @param bookName * @return */ String addBook(String bookName); }
- BookDaoImpl.java
/** * Interface implementation class */ @Repository public class BookDaoImpl implements BookDao { /** * Add book interface * * @param bookName * @return */ public String addBook(String bookName) { return "Add book<" + bookName + ">Success"; } }
- 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("Establish User object"); } public User(String msg) { System.out.println("Establish User object" + msg); } public void show() { System.out.println("A student object!"); } }
- 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("Success"); } }
Test class
@org.junit.Test public void testBook(){ ApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class); BookService bookService = ctx.getBean(BookService.class); bookService.storeBook("Jay Chou"); User user = ctx.getBean("getUser",User.class); user.show(); }
Operation result
Create User object User object created successfully Add the book Jay Chou successfully A student object!
@Configuration
@Configuration is equivalent to < beans / >
@ComponentScan
@Component scan is equivalent to the context: component scan in the configuration file
@Bean
@Bean is equivalent to < bean / > and can only be annotated on methods and annotations. It is generally used on methods
Method name is equivalent to id
Note that getUser is used to get user above, because the method name is equivalent to id
@Value get properties configuration
- Create a new dev.properties file
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 and set methods omitted }
test
@org.junit.Test public void testBoyUtil(){ ApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class); BoyUtil boyUtil = ctx.getBean(BoyUtil.class); System.out.println(boyUtil); }
Output result
Create User object User object created successfully BoyUtil{name='kevin', age='20'}
OK, the spring IOC foundation will come to an end for the time being. Have fun
Code: https://github.com/runzhenghengbin/spring-study
Reference resources: https://www.cnblogs.com/best/p/5727935.html#_label3