1. Create User entity class.
1 2 3 4 5 6 | @Data
public class User {
private String username;
private String password;
private Integer age;
}
|
Copy after login
2. Create UserDao to simulate database interaction.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class UserDao{
public List<user> queryUserList() {
List<user> result = new ArrayList<user>();
for (int i = 1;i <p><strong>3. Write UserService to implement User data operation business logic. </strong></p>
<pre class = "brush:php;toolbar:false" >@service
public class UserService{
@Autowired
private UserDao userDao;
public List<user> queryUserList() {
return this.userDao.queryUserList();
}
}</user>
|
Copy after login
4. Write SpringConfig to instantiate the Spring container.
1 2 3 4 5 6 7 8 9 | @Configuration
@ComponentScan(basePackages = "cn.my.springboot.javaconfig" )
public class SpringConfig {
@Bean
public UserDao getUserDao() {
return new UserDao();
}
}</bean>
|
Copy after login
5. Write a test method to start the Spring container.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(SpringConfig. class );
UserService userService = context.getBean(UserService. class );
List<user> list = userService.queryUserList();
for (User user : list) {
System.out.println(user.getUsername() + "|" user.getPassword() + "|" user.getAge());
context.destroy;
}
}
}</user>
|
Copy after login
Test results:

You can use java code to perfectly replace the XML configuration file.
As for the structure, please don’t be clear, it’s all in the eye of the beholder.
The above is the detailed content of What is the configuration method of java in springboot. For more information, please follow other related articles on the PHP Chinese website!