Spring的Java配置方式是經過 @Configuration 和 @Bean 這兩個註解實現的:java
一、@Configuration 做用於類上,至關於一個xml配置文件;web
二、@Bean 做用於方法上,至關於xml配置中的<bean>;spring
pom.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.0</modelVersion> <groupId>demo.springboot</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.11.RELEASE</version> </dependency> <!-- 鏈接池 --> <dependency> <groupId>com.jolbox</groupId> <artifactId>bonecp-spring</artifactId> <version>0.8.0.RELEASE</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <!-- 資源文件拷貝插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- java編譯插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <!-- 配置Tomcat插件 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
pojoapache
package demo.pojo; public class User { private String username; private String password; private Integer age; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
daotomcat
package demo.dao; import java.util.ArrayList; import java.util.List; import demo.pojo.User; public class UserDao { public List<User> queryUserList(){ List<User> result = new ArrayList<User>(); // 模擬數據庫的查詢 for (int i = 0; i < 10; i++) { User user = new User(); user.setUsername("username_" + i); user.setPassword("password_" + i); user.setAge(i + 1); result.add(user); } return result; } }
servicespringboot
package demo.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import demo.dao.UserDao; import demo.pojo.User; @Service public class UserService { @Autowired private UserDao dao; public List<User> queryUserList() { return dao.queryUserList(); } }
config:java配置mvc
package demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import demo.dao.UserDao; @Configuration //至關於一個spring.xml @ComponentScan(basePackages="demo")//配置掃描包 public class SpringConfig { @Bean // 經過該註解來代表是一個Bean對象,至關於xml中的<bean> public UserDao getUserDao(){ return new UserDao(); // 直接new對象 } }
測試類maven
package test; import java.util.List; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import demo.config.SpringConfig; import demo.pojo.User; import demo.service.UserService; public class SpringBootTest { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); UserService userService = context.getBean(UserService.class); List<User> queryUserList = userService.queryUserList(); for (User user : queryUserList) { System.out.println(user); } //銷燬容器 context.destroy(); } }
使用Java代碼就完美的替代xml配置文件,而且結構更加的清晰。測試