12.Spring的入门配置

 

Spring的入门配置Demo

vo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.course.vo;

public class User {

private Integer id;
private String name;
private String address;

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", address=" + address + "]";
}
}

Dao

1
2
3
4
5
6
7
8
package com.course.dao;

import com.course.vo.User;

public interface UserDao {

public User getUser(Integer id);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.course.dao.impl;

import com.course.dao.UserDao;
import com.course.vo.User;

public class UserDaoImpl implements UserDao {

@Override
public User getUser(Integer id) {
User user = new User();
user.setId(id);
user.setName("Tom" + id);
user.setAddress("location" + id);
return user;
}

}

Service

1
2
3
4
5
6
7
8
package com.course.service;

import com.course.vo.User;

public interface UserService {

public User getUserInfo(Integer id);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.course.service.impl;

import com.course.dao.UserDao;
import com.course.service.UserService;
import com.course.vo.User;

public class UserServiceImpl implements UserService {

private UserDao userDao;

// set()
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

@Override
public User getUserInfo(Integer id) {
User user = userDao.getUser(id);
return user;
}

}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.course.controller;

import com.course.service.UserService;
import com.course.vo.User;

public class UserController {

private UserService userService;

// set()
public void setUserService(UserService userService) {
this.userService = userService;
}

public User getUserById(Integer id) {
User user = userService.getUserInfo(id);
return user;
}
}

test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.course.controller.UserController;
import com.course.vo.User;

public class TestMain {

public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println(applicationContext);
// org.springframework.context.support.ClassPathXmlApplicationContext@2a742aa2: startup date [Thu Nov 25 16:31:47 CST 2021]; root of context hierarchy

UserController userController = (UserController) applicationContext.getBean("userController");
User user = userController.getUserById(1);
System.out.println(user);
// User [id=1, name=Tom1, address=location1]
}
}

applicationContext.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<!-- 头文件 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="userDao" class="com.course.dao.impl.UserDaoImpl">

</bean>

<bean id="userService" class="com.course.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>

<bean id="userController" class="com.course.controller.UserController">
<property name="userService" ref="userService"></property>
</bean>

</beans>

log4j.properties

1
2
3
4
5
6
7
8
# Global logging configuration
log4j.rootLogger=INFO, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n