15.使用注解

 

使用注解

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
18
19
20
package com.course.dao.impl;

import org.springframework.stereotype.Repository;

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

@Repository
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
23
24
25
26
27
package com.course.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class UserServiceImpl implements UserService {

@Autowired
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
20
21
22
23
24
package com.course.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

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

@Controller
public class UserController {

@Autowired
private UserService userService;

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

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

applicationContext.xml

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

<!--
<context:component-scan base-package="com.course.dao.impl"></context:component-scan>
<context:component-scan base-package="com.course.service.impl"></context:component-scan>
<context:component-scan base-package="com.course.controller"></context:component-scan>

<context:component-scan base-package="com.course.dao.impl,com.course.service.impl,com.course.controller"></context:component-scan>
-->

<context:component-scan base-package="com.course"></context:component-scan>
</beans>

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);

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

注解说明

注解 位置 作用
@Repository 数据访问层 创建对象
@Service 服务层 创建对象
@Controller 控制层 创建对象
@Component 任何位置 创建对象,当一个类没有确定的分类时,可以使用该注解
@Autowired 属性、方法、参数、构造方法 自动注入,当放在方法上时,注入对象与方法名无关,取决于参数名