2025-05-15
๊ตฌ๋ถ | STS3 (Spring 5) ํ๊ฒฝ | Spring Boot 3.x ํ๊ฒฝ |
---|---|---|
์ค์ ์คํ์ผ | XML ๋๋ Java Config ํผํฉ | Java Config ์ค์ฌ, ์ต์ XML ์ฌ์ฉ |
๋ถํธ์คํธ๋ฉ ๋ฐฉ์ | ์๋ ์ค์ (DispatcherServlet ๋ฑ ์๋ ๊ตฌ์ฑ) | ์๋ ์ค์ (Spring Boot Auto Configuration) |
Spring Security | ๋ณดํต 5.x ๋ฒ์ ์ฌ์ฉ | ๊ธฐ๋ณธ์ ์ผ๋ก 6.x ์ด์ ์ฌ์ฉ |
์๋ธ๋ฆฟ ์คํ | Servlet 3.0 ๋๋ 3.1 | Servlet 5.0 ์ด์ ์๊ตฌ |
JDK ๋ฒ์ | 8 ~ 11 | 17 ์ด์ (๊ธฐ๋ณธ ์๊ตฌ์ฌํญ) |
build.gradle
์ฃผ์ ์ค์ plugins {
id 'java'
id 'org.springframework.boot' version '3.4.5'
id 'io.spring.dependency-management' version '1.1.7'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
dependencies {
// DB
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
runtimeOnly 'com.mysql:mysql-connector-j'
// Security
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
// Web, Thymeleaf
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
// Lombok
annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'
// Test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
spring.application.name=demo
server.port=8090
# UTF-8 ํํฐ ์ค์
spring.servlet.filter.encoding.filter-name=encodingFilter
spring.servlet.filter.encoding.filter-class=org.springframework.web.filter.CharacterEncodingFilter
spring.servlet.filter.encoding.init-param.encoding=UTF-8
spring.servlet.filter.encoding.init-param.forceEncoding=true
spring.servlet.filter.encoding.url-pattern=/*
@Configuration
public class DataSourceConfig {
@Bean
public HikariDataSource dataSource() {
HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/testdb");
dataSource.setUsername("root");
dataSource.setPassword("1234");
return dataSource;
}
}
@Configuration
@EntityScan(basePackages = {"com.example.demo.domain.entity"})
@EnableJpaRepositories(
basePackages = {"com.example.demo.domain.repository"},
transactionManagerRef = "jpaTransactionManager"
)
public class JpaConfig {
@Autowired
private DataSource dataSource;
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setDataSource(dataSource);
factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factory.setPackagesToScan("com.example.demo.domain.entity");
Map<String, Object> props = new HashMap<>();
props.put("hibernate.hbm2ddl.auto", "update");
props.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
props.put("hibernate.show_sql", true);
props.put("hibernate.format_sql", true);
factory.setJpaPropertyMap(props);
return factory;
}
}
@Configuration
@EnableTransactionManagement
public class TxConfig {
@Autowired
private DataSource dataSource;
@Bean(name="jpaTransactionManager")
public JpaTransactionManager jpaTransactionManager(EntityManagerFactory emf) {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(emf);
txManager.setDataSource(dataSource);
return txManager;
}
}
src/
โโโ main/
โโโ java/com/example/demo/
โ โโโ config/
โ โ โโโ DataSourceConfig.java
โ โ โโโ JpaConfig.java
โ โ โโโ TxConfig.java
โ โโโ controller/
โ โโโ domain/
โ โโโ dto/
โ โโโ entity/
โ โโโ repository/
โโโ resources/
โโโ static/
โโโ templates/
โ โโโ home.html
โ โโโ join.html
โ โโโ login.html
โโโ application.properties
http://localhost:8090
) Spring Security ๊ธฐ๋ณธ ๋ก๊ทธ์ธ ์ฐฝ ์ถ๋ ฅuser
Using generated security password: **************
SecurityConfig
์ค์ ๋ฐ ์ฌ์ฉ์ ์ธ์ฆ ์ฒ๋ฆฌ๋ก ํ์ฅ ์์