Spring-boot

advantage :

1. production ready application 

2. complex configuartion is reduced 

3. pom.xml -all dependency define via ths file .

4. used for standalone and web apps .

5. used for microservices -***

6.@EnableAutoConfiguration automatically configures the Spring application based on its included jar files
 note:
  6.1- Auto-configuration is usually applied based on the classpath and the defined beans
  
  6.2-@EnableAutoConfiguration has a exclude attribute to disable an auto-configuration explicitly otherwise we can simply exclude it from the pom.xml, for example if we donot want Spring to configure the tomcat then exclude spring-bootstarter-tomcat from spring-boot-starter-web.
   eg. like as follwing :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>

</dependency> 

7. @ComponentScan

      beans will be auto-detected by @ComponentScan annotation. There are some annotations which make beans auto-detectable like @Repository , @Service, @Controller, @Configuration, @Component
e.g

8. logger
applicaion.properties
eg.
logging.level.org.springframework.web=info
logging.level.com.mkyong=info
# Logging pattern for the console
logging.pattern.console= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
# Logging pattern for file
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
logging.file=f:/logs/samota.log
//java code 
{
private static final Logger log = LoggerFactory.getLogger(SamotaApplication.class);
}

9. config beans
eg.
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(DB_DRIVER);
dataSource.setUrl(DB_URL);
dataSource.setUsername(DB_USERNAME);
dataSource.setPassword(DB_PASSWORD);
return dataSource;
}

@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
// sessionFactoryBean.setPackagesToScan(ENTITYMANAGER_PACKAGES_TO_SCAN);
Properties hibernateProperties = new Properties();
sessionFactoryBean.setDataSource(dataSource());
sessionFactoryBean.setHibernateProperties(hibernateProperties);
sessionFactoryBean.setMappingResources(HIBERNATE_config_agent);
sessionFactoryBean.setMappingResources(HIBERNATE_config_test);
//sessionFactoryBean.setMappingResources(HIBERNATE_config_hello);
// sessionFactoryBean.setMappingDirectoryLocations(list);

// sessionFactoryBean.setJtaTransactionManager(JPA_TX);
return sessionFactoryBean;

  @Bean
    public PlatformTransactionManager transactionManager(final EntityManagerFactory emf) {
        final JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);
        transactionManager.setDataSource(dataSource());
        //transactionManager.setJpaDialect(jpaDialect());
        return transactionManager;
     
    }//for jpa
 @PersistenceContext
    private EntityManager entityManager;

//for hibernate 
@Autowired
private SessionFactory sessionfactory;
then ->
   entityManager.createNativeQuery("update user set name="xyz" where id=?);
or
sessionfactory.createNativeQuery("update user set name="xyz" where id=?);// in case of hibernate.

//dynamic setParameter setting in jpa/hibernate query

Query qry1 = session.createNativeQuery(" select * from user a ,address b where a.id=?");
qry1.setParameter(1, 105);
more..e.g
Query qry1 = session.createNativeQuery(" select * from user a ,address b where a.id=? and a.name=?");
qry1.setParameter(1, 104);
qry1.setParameter(2, "sam");


Example of a hibernate.cfg file with Wildcard support - 

<?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> <!-- connection details --> <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.url">jdbc:postgresql://localhost/myDatabase</property> <property name="hibernate.connection.username">myUser</property> <property name="hibernate.connection.password">myPass</property> <!-- @see: http://community.jboss.org/wiki/HibernateCoreMigrationGuide36 --> 
 <property name="hibernate.jdbc.use_streams_for_binary">false</property> 
 <!-- mappings for annotated classes --> 
 <mapping class="uk.org.adamretter.org.uk.someapp.web.orm.generated.*"/>
 </session-factory> 
</hibernate-configuration>

// pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.firstdemo</groupId>
<artifactId>samota</artifactId>
<version>1.0</version>
<name>samota</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<maven-jar-plugin.version>2.6</maven-jar-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>

</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

</project>

poc:
1. load req beans on system startup
2. give request param via config file so minimal changes on code level
3.takes the some request params list from ui-api and put thos values in config attribute values .
  eg. location=?
values should come from ui and set into location=jpr;

Note:process
On new project check-out/project-setup process on local system from git repos.
1. first check out the project in out local system
2.import the project general/maven
3.Go to Project -prop setting
   3.1  -changes/add the jdk specific version as per need or project documents.
   3.2  -check the java-compiler version as per jdk .jars
   3.3  -add the required project -jars/libs.
   3.4 - or check the maven -pom.xml id project is maven based .
   3.5 - change the all the project/System properties/messages/labels path
            as per our local user project directory
              E.g.- Web.xml or other referenced eg. file-servlet.xml
                      <context-param>
                             <param-name>path</param-name>
                            <param-value>D:\Estel_soft\CRMDBTH\admin.xml</param-value>
                    </context-param>
4. check other properties file /resource location or db/port/host infomation if any as per document.
5. Give/specify the project dependency in case of multi-Module project .
     5.1-as per project documents .
     5.2-might be one module is dependent of another regarding to .jar file or .java files.

//Read the data from json object (coming frm ui) into java controller class
//use json api.



spring boot security

1.
add the spring-boot-starter-security maven dependency in pom.xml

2. put the annotation as below  on configuration class where datasource and factory beans are define .
   e.g @enablewebsecurity
3. extends WebSecurityConfigurerAdapter

4.then override the configure() method
etc....

spring boot aop
Desc: used to provide cross cutting concern in entire application .
Implementation steps : 1. add the spring-boot-starter-aop maven dependency 
2. Create the Aspect class that define the joinpoints and the pointcuts. 
     2.1   @Before -put the logic 
   2.2 @after -put the logic




Comments

Popular posts from this blog

Angular 2 & 2+

Ajax calling to controller

How spring flow