Java框架教程
Java框架是为了简化Java应用程序开发而设计的一套可重用的组件和工具。本教程将介绍Java生态系统中最流行的几个框架,帮助你快速上手这些强大的工具。
一、Spring框架
1.1 Spring框架概述
Spring是一个轻量级的Java企业应用框架,它提供了全面的基础设施支持,用于开发Java应用程序。Spring框架的核心是控制反转(IoC)和面向切面编程(AOP)。
1.2 Spring的核心模块
- Spring Core:提供IoC容器
- Spring Context:提供应用上下文
- Spring AOP:提供面向切面编程功能
- Spring DAO:提供数据访问抽象层
- Spring ORM:提供对象关系映射集成
- Spring Web:提供Web应用开发支持
- Spring MVC:提供MVC框架
1.3 控制反转(IoC)和依赖注入(DI)
控制反转是Spring框架的核心概念,它将对象的创建和依赖管理的控制权从应用代码转移到Spring容器。
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
|
// 定义Bean
public class UserService {
private UserDao userDao;
// 设置依赖(构造函数注入)
public UserService(UserDao userDao) {
this.userDao = userDao;
}
// 或者使用setter方法注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public User getUserById(int id) {
return userDao.findById(id);
}
}
public class UserDao {
public User findById(int id) {
// 模拟数据库操作
return new User(id, "User " + id);
}
}
|
1.3.1 XML配置方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!-- applicationContext.xml -->
<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">
<!-- 定义UserDao bean -->
<bean id="userDao" class="com.example.UserDao"/>
<!-- 定义UserService bean,使用构造函数注入 -->
<bean id="userService" class="com.example.UserService">
<constructor-arg ref="userDao"/>
</bean>
<!-- 或者使用setter注入 -->
<!-- <bean id="userService" class="com.example.UserService">
<property name="userDao" ref="userDao"/>
</bean> -->
</beans>
|
1.3.2 注解配置方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
// 启用组件扫描
@Configuration
@ComponentScan("com.example")
public class AppConfig {
// 配置类
}
// 组件类
@Component
public class UserDao {
// 实现
}
@Service
public class UserService {
@Autowired // 自动注入
private UserDao userDao;
// 方法实现
}
|
1.4 面向切面编程(AOP)
AOP允许开发者将横切关注点从业务逻辑中分离出来,以提高代码的模块化程度。
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
|
// 定义切面
@Aspect
@Component
public class LoggingAspect {
// 定义切点
@Pointcut("execution(* com.example.service.*.*(..))")
public void serviceMethods() {}
// 前置通知
@Before("serviceMethods()")
public void logBefore(JoinPoint joinPoint) {
System.out.println("执行方法前: " + joinPoint.getSignature().getName());
}
// 后置通知
@After("serviceMethods()")
public void logAfter(JoinPoint joinPoint) {
System.out.println("执行方法后: " + joinPoint.getSignature().getName());
}
// 环绕通知
@Around("serviceMethods()")
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = joinPoint.proceed();
long endTime = System.currentTimeMillis();
System.out.println("方法执行时间: " + (endTime - startTime) + "ms");
return result;
}
}
|
二、Spring Boot
2.1 Spring Boot概述
Spring Boot是由Pivotal团队提供的框架,用于简化Spring应用的初始搭建和开发过程。它使用"约定优于配置"的理念,提供了自动配置、嵌入式服务器等特性,让开发者可以快速创建独立的、生产级别的Spring应用。
2.2 Spring Boot的主要特性
- 自动配置:根据类路径中的依赖自动配置Spring应用
- 独立运行:内嵌Tomcat、Jetty或Undertow服务器
- 无代码生成和XML配置:使用注解而非XML配置
- 提供生产就绪的功能:如指标、健康检查和外部化配置
- 简化依赖管理:提供starter依赖,简化Maven和Gradle配置
2.3 创建Spring Boot项目
2.3.1 使用Spring Initializr
- 访问 https://start.spring.io/
- 选择项目类型(Maven/Gradle)、语言、Spring Boot版本
- 添加所需依赖(如Spring Web、Spring Data JPA等)
- 生成项目并下载
2.3.2 项目结构
1
2
3
4
5
6
7
|
src/main/java/com/example/demo/
├── DemoApplication.java // 应用入口类
├── controller/ // 控制器
├── service/ // 服务层
├── repository/ // 数据访问层
├── model/ // 实体类
└── config/ // 配置类
|
2.4 Spring Boot应用示例
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
// 应用入口类
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
// 控制器
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> getAllUsers() {
return userService.findAll();
}
@GetMapping("/{id}")
public User getUserById(@PathVariable Long id) {
return userService.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("User not found with id: " + id));
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public User createUser(@RequestBody User user) {
return userService.save(user);
}
@PutMapping("/{id}")
public User updateUser(@PathVariable Long id, @RequestBody User userDetails) {
User user = userService.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("User not found with id: " + id));
user.setName(userDetails.getName());
user.setEmail(userDetails.getEmail());
return userService.save(user);
}
@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteUser(@PathVariable Long id) {
User user = userService.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("User not found with id: " + id));
userService.delete(user);
}
}
// 服务层
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> findAll() {
return userRepository.findAll();
}
public Optional<User> findById(Long id) {
return userRepository.findById(id);
}
public User save(User user) {
return userRepository.save(user);
}
public void delete(User user) {
userRepository.delete(user);
}
}
// 数据访问层
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
// JPA自动实现基本CRUD操作
List<User> findByNameContaining(String name);
}
// 实体类
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
private String name;
@NotBlank
@Email
private String email;
// getter和setter方法
}
// 配置文件 (application.properties)
# 服务器配置
server.port=8080
server.servlet.context-path=/demo
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# JPA配置
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
|
三、MyBatis
3.1 MyBatis概述
MyBatis是一个优秀的持久层框架,它支持自定义SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。
3.2 MyBatis的主要特性
- 简单易学:相比Hibernate,MyBatis更加简单和轻量级
- SQL控制:开发者可以完全控制SQL的编写,适合复杂查询
- 灵活映射:支持各种复杂对象的映射关系
- 动态SQL:可以根据条件动态生成SQL语句
3.3 MyBatis的配置和使用
3.3.1 Maven依赖
1
2
3
4
5
6
7
8
9
10
|
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
|
3.3.2 MyBatis配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<!-- mybatis-config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mapper/UserMapper.xml"/>
</mappers>
</configuration>
|
3.3.3 Mapper接口
1
2
3
4
5
6
7
8
|
public interface UserMapper {
List<User> findAll();
User findById(Long id);
int insert(User user);
int update(User user);
int delete(Long id);
List<User> findByName(String name);
}
|
3.3.4 Mapper XML文件
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
33
34
35
36
|
<!-- UserMapper.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
<resultMap id="UserMap" type="com.example.model.User">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="email" column="email"/>
</resultMap>
<select id="findAll" resultMap="UserMap">
SELECT * FROM users
</select>
<select id="findById" resultMap="UserMap">
SELECT * FROM users WHERE id = #{id}
</select>
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
INSERT INTO users (name, email) VALUES (#{name}, #{email})
</insert>
<update id="update">
UPDATE users SET name = #{name}, email = #{email} WHERE id = #{id}
</update>
<delete id="delete">
DELETE FROM users WHERE id = #{id}
</delete>
<select id="findByName" resultMap="UserMap">
SELECT * FROM users WHERE name LIKE CONCAT('%', #{name}, '%')
</select>
</mapper>
|
3.3.5 MyBatis使用示例
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
33
34
35
36
37
|
public class MyBatisDemo {
private SqlSessionFactory sqlSessionFactory;
public MyBatisDemo() {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public List<User> getAllUsers() {
try (SqlSession session = sqlSessionFactory.openSession()) {
UserMapper mapper = session.getMapper(UserMapper.class);
return mapper.findAll();
}
}
public User getUserById(Long id) {
try (SqlSession session = sqlSessionFactory.openSession()) {
UserMapper mapper = session.getMapper(UserMapper.class);
return mapper.findById(id);
}
}
public void addUser(User user) {
try (SqlSession session = sqlSessionFactory.openSession()) {
UserMapper mapper = session.getMapper(UserMapper.class);
mapper.insert(user);
session.commit();
}
}
// 其他方法...
}
|
3.4 Spring Boot整合MyBatis
3.4.1 Maven依赖
1
2
3
4
5
|
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
|
3.4.2 配置文件 (application.properties)
1
2
3
4
5
6
7
8
|
# 数据库配置
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
# MyBatis配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.model
|
3.4.3 应用入口类
1
2
3
4
5
6
7
|
@SpringBootApplication
@MapperScan("com.example.mapper") // 扫描Mapper接口
public class MyBatisApplication {
public static void main(String[] args) {
SpringApplication.run(MyBatisApplication.class, args);
}
}
|
四、Hibernate
4.1 Hibernate概述
Hibernate是一个开源的对象关系映射(ORM)框架,它对JDBC进行了轻量级的对象封装,使得Java开发人员可以使用对象编程思维来操作数据库。
4.2 Hibernate的主要特性
- 对象关系映射:自动完成Java对象和数据库表之间的映射
- 数据库无关性:支持多种数据库,减少数据库迁移的工作量
- 缓存机制:提供一级缓存和二级缓存,提高性能
- 事务管理:支持声明式事务管理
- HQL查询语言:提供面向对象的查询语言
4.3 Hibernate的配置和使用
4.3.1 Maven依赖
1
2
3
4
5
|
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.5.7.Final</version>
</dependency>
|
4.3.2 Hibernate配置文件
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
33
34
|
<!-- hibernate.cfg.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 数据库连接设置 -->
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo?useSSL=false&serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<!-- JDBC连接池设置 -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<!-- SQL方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 打印SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化SQL -->
<property name="hibernate.format_sql">true</property>
<!-- 自动创建表 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 映射文件 -->
<mapping class="com.example.model.User"/>
</session-factory>
</hibernate-configuration>
|
4.3.3 实体类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "email", nullable = false, unique = true)
private String email;
// getter和setter方法
}
|
4.3.4 Hibernate使用示例
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// 创建SessionFactory
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
public class HibernateDemo {
public void saveUser(User user) {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(user);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
} finally {
session.close();
}
}
public User getUserById(Long id) {
Session session = HibernateUtil.getSessionFactory().openSession();
User user = null;
try {
user = session.get(User.class, id);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return user;
}
public List<User> getAllUsers() {
Session session = HibernateUtil.getSessionFactory().openSession();
List<User> users = null;
try {
Query<User> query = session.createQuery("FROM User", User.class);
users = query.list();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
return users;
}
// 其他方法...
}
|
五、Spring MVC
5.1 Spring MVC概述
Spring MVC是Spring框架的一个模块,用于构建Web应用程序。它提供了一个模型-视图-控制器(MVC)架构,用于开发灵活、松耦合的Web应用。
5.2 Spring MVC的核心组件
- DispatcherServlet:前端控制器,负责接收请求并分发给相应的处理器
- HandlerMapping:将请求映射到处理器
- Controller:处理请求的控制器
- ModelAndView:封装模型数据和视图信息
- ViewResolver:解析视图名称,定位到具体的视图实现
5.3 Spring MVC的配置和使用
5.3.1 Maven依赖
1
2
3
4
5
|
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.10</version>
</dependency>
|
5.3.2 web.xml配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<!-- web.xml -->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- 配置DispatcherServlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
|
5.3.3 Spring MVC配置文件
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
|
<!-- dispatcher-servlet.xml -->
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 启用注解驱动 -->
<mvc:annotation-driven/>
<!-- 组件扫描 -->
<context:component-scan base-package="com.example.controller"/>
<!-- 静态资源处理 -->
<mvc:resources mapping="/resources/**" location="/resources/"/>
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
|
5.3.4 控制器示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@Controller
public class HomeController {
@RequestMapping("/")
public String home(Model model) {
model.addAttribute("message", "Hello, Spring MVC!");
return "home";
}
@RequestMapping("/users")
public String listUsers(Model model) {
List<String> users = Arrays.asList("张三", "李四", "王五");
model.addAttribute("users", users);
return "userList";
}
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String addUser(@RequestParam("name") String name, Model model) {
// 处理添加用户的逻辑
model.addAttribute("message", "用户 " + name + " 添加成功!");
return "result";
}
}
|
六、Spring Security
6.1 Spring Security概述
Spring Security是一个功能强大且高度可定制的身份验证和访问控制框架,用于保护基于Spring的应用程序。
6.2 Spring Security的主要特性
- 身份验证:验证用户身份
- 授权:控制用户对系统资源的访问
- 保护防止常见攻击:如CSRF(跨站请求伪造)、XSS(跨站脚本)等
- 会话管理:管理用户会话
- 集成其他认证系统:如LDAP、OAuth、OpenID等
6.3 Spring Security的配置和使用
6.3.1 Maven依赖
1
2
3
4
|
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
|
6.3.2 Spring Security配置类
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
33
|
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home", "/register").permitAll() // 允许所有人访问
.antMatchers("/admin/**").hasRole("ADMIN") // 只有管理员可以访问
.anyRequest().authenticated() // 其他请求需要认证
.and()
.formLogin()
.loginPage("/login") // 自定义登录页面
.permitAll()
.and()
.logout()
.permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
|
6.3.3 UserDetailsService实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("User not found with username: " + username));
return new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
getAuthorities(user.getRoles()));
}
private Collection<? extends GrantedAuthority> getAuthorities(Set<Role> roles) {
return roles.stream()
.map(role -> new SimpleGrantedAuthority("ROLE_" + role.getName()))
.collect(Collectors.toList());
}
}
|
七、其他常用Java框架
7.1 Spring Cloud
Spring Cloud是一系列框架的集合,用于快速构建分布式系统中的一些常见模式,如配置管理、服务发现、断路器、智能路由、微代理、控制总线等。
主要组件包括:
- Spring Cloud Config:配置管理
- Spring Cloud Netflix:包含Eureka、Hystrix、Zuul等组件
- Spring Cloud Gateway:API网关
- Spring Cloud Circuit Breaker:断路器
- Spring Cloud Sleuth:分布式跟踪
7.2 Quartz
Quartz是一个功能丰富的开源任务调度库,可以集成到几乎任何Java应用程序中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
// 创建调度器
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
// 开始调度器
scheduler.start();
// 创建作业
JobDetail job = JobBuilder.newJob(MyJob.class)
.withIdentity("job1", "group1")
.build();
// 创建触发器
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("trigger1", "group1")
.startNow()
.withSchedule(SimpleScheduleBuilder.simpleSchedule()
.withIntervalInSeconds(10)
.repeatForever())
.build();
// 调度作业
scheduler.scheduleJob(job, trigger);
|
7.3 Apache Shiro
Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证、授权、加密和会话管理功能。
7.4 Log4j/SLF4J
日志框架,用于记录应用程序的日志信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
// SLF4J使用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogDemo {
private static final Logger logger = LoggerFactory.getLogger(LogDemo.class);
public void doSomething() {
logger.debug("Debug message");
logger.info("Info message");
logger.warn("Warn message");
logger.error("Error message", new Exception("Test exception"));
}
}
|
八、学习建议
- 循序渐进:先掌握一个框架,再学习其他框架
- 理解原理:不要只停留在API层面,要理解框架的设计思想和实现原理
- 多做项目:通过实际项目应用框架,加深理解
- 阅读官方文档:官方文档是最权威的学习资源
- 关注版本更新:框架会不断更新,关注新版本的特性和变化
结语
本教程介绍了Java生态系统中几个主流的框架,包括Spring、Spring Boot、MyBatis、Hibernate、Spring MVC、Spring Security等。这些框架各有特点,适用于不同的场景。
选择合适的框架对于项目的成功至关重要。在实际开发中,我们通常会组合使用多个框架,如Spring Boot + MyBatis、Spring Boot + Spring MVC等,以充分利用各个框架的优势。
希望本教程对你的Java框架学习有所帮助!