import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig
{
/*
Here we provide the different configurations for example....
Bean Configurations, Database Configurations, MVC Configurations, Security Configurations etc
*/
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BeanConfig
{
// Below @Bean annotation is used to create Student class Bean
@Bean
public Student stdId() // Here method name is the bean-id / bean-name
{
// It will return the Student object
return new Student();
}
}
//package and import statements
@Configuration
public class DatabaseConfig
{
@Bean
public DataSource dataSource()
{
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mydb");
dataSource.setUsername("database-username");
dataSource.setPassword("database-password");
return dataSource;
}
}
//package and import statements
@Configuration
public class WebConfig implements WebMvcConfigurer
{
@Override
public void addViewControllers(ViewControllerRegistry registry)
{
registry.addViewController("/home").setViewName("home");
}
@Override
public void addCorsMappings(CorsRegistry registry)
{
registry.addMapping("/**").allowedOrigins("https://example.com");
}
}
//package and import statements
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
protected void configure(HttpSecurity http) throws Exception
{
http
.authorizeRequests()
.antMatchers("/public/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin();
}
}
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.