0% found this document useful (0 votes)
17 views

Spring

Uploaded by

Himanshu Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Spring

Uploaded by

Himanshu Yadav
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

IOC, Dependency injection

Spring core - Core*, beans* , Context* , spEL


AOP, Aspect , Instrumentation , Messaging
Data Acess/ Integration -> JDBC**, ORM** ->Integration tool(Hibernet) , JMS(java
Messaging), OXM(object xml mapping)

***Web Module -> Wev , Servlet , Portlet , WevSocket --- MVC

Test Module -> unit testing , integration ( Junit)

-- spring frame work make java application - loosely coupled ( dependecny


injection)
Ex. for example in a class we need an object of another class , so we don't need
to make a
object inti. like new obj() , spring will we do this when needed
loose coupling between components ( using xml files )
-- inversion of control -> it controls dependecy injection
-- dependency injection -> it is a design pattern

-->> Spring IOC Container


-- creating objects , hold them in memory , inject an object into another
object ( life cycle)
we will give
--> beans <--- Application code
--> xml configration <---

Application Context -> represents IOC Container , also has bean factory
->> ***ClassPathXMLApplicationContext*** -- it searches for xml config. in
java classPath
->> AnnotationConfigApplicationContext
->> FileSystemXMLApplicationContext

Dependency injection methods -> setter Injection or Construction injection

Q. Where we declare beans and its dependency -> in XML file , <beans> </beans>

/* setting up spring project by creating a maven project


1. create maven project
2. add dependencies -> spring core , spring context
3. create beans - java pojo
4. creating configuration files -> config.xml
5. setting injection
6. main class : which can pull the object and use
*/

/*
--> adding dependecies
in the pom.xml file --
declare all the dependecies , like spring-core , spring-context(version
should be same for both
*/

/* creating beans --> these are made in configuration.xml file ( refer docs of
spring)
** beans is like object -> when we need some object in some class throught this
bean it is automatically passed

-- how to create a bean

<bean name="object_name" class="package.className">


<property name="" value=""/ >
name = data member of class , value = its value
</bean>

or
<bean class="" name="objectName" p:dataMemberName="its value"/>
*/

/*
pulling object in main class using beans

new ClassPathXmlApplicationContext("xml file path");


className emp11 = (className) context.getBean("objectName");
*/

-------------- reference injection --------------

<bean class="" name="">


<property name="">
<ref bean="" >
</property>
</bean>

-----------------------------------------------------

--------------- constructor injection -------------

<bean name="" class="" >


<constructor-arg value=""/>
</bean>

------------------------------------------------------

-------------life Cycles method of bean ------------------

init() -> ........ -> destroy(); // can change names of these methods

->> configuration technique -


1. Xml
2. Spring Interface
3. Annotation

*** Using XML files


<bean class="" name="" init-method="" destroy-method="">
<property name="" value""/>
</bean>
***

*** spring interfaces


implements InitializingBean,DisposalBean
now write functions
***

*** Annotation
@PostConstruct
@PreDestroy

***

----------------AutoWiring-----------------------
-> only works for object types(automaticaly inject depedencies/without xml
files)
-> not works on primitive and string
can be done in two ways ->
XML -> (does not autorewire)(default), byName, byType, constructor
Annotations -> @Autowired

@Autowired
used before will be called , ( constructor , setter, property)
@Qualifier("objectName") // name of injecting object
--------------------------------------------------------------------

----------------Spring Standalone Collections----------------------

-> why wee need


let say there a list , which we have defined in a bean properties, but if we
want to use that list some else we need to copy paste it ,
so we define it outside of bean with a id

Ex.
<util:List id="" class="java.util.LinkedList">
<value></value>
</util:List>
-> in bean <property name="" ref="idName"/>

------------------------------------------------------------

**************stereotype annotations*********************

----> get rid of bean,


in xml file define -->> <context:component-scan base-package="packageName"/>

before class definition -> @Component("objectName")


before data members -> @Value("data")
@Value("#{CollectionID}")

----------------------------------------------------------------------

*******************************Bean Scope******************
Syntax -> @Component , @Scope("prototype")
1. Singleton
2. prototype
3.request // web
4.session // web
5.globalsession // portlet application

1. Singleton -> every time spring gives same object when bean is called
2. proptotype -> create a new object every time

****************************************************************

SpEL -> spring expression language

1. parsing and excuting expressions ->>> @Value("#{ expression(object/static


method, variable }")

***************************************************************

invoke static mehtod and variable , in the class in Value("#{}")

T(class).method(param) Ex. T(java.lang.Math).sqrt(25)


T(class).variable Ex. T(java.lang.Math).PI

creating objects Ex. new java.lang.String('hello')


***********************************************************************************
************

** get rid from XML Files **


> make a java class and make it as
@Configuration
@ComponentScan(basePackage ="")
how to access ->
ApplicationContext con =
AnnotationConfigApplicationContext(ConfigClass.class);

-> now remove @Componet annoation


in the config class file
ex.
@Bean
public Student getStudent(){
Student st = new Student();
return st;
}

**********************************************************************

Spring JDBC
It is API to perform operation with database , class JdbcTemplate

Problem with JDBC ->


1. need to write a lot of code ( connection open , statement , execute ,
close)
2. exception handling , sql exeption problem
3. databasic logic is a time consuming task
DriverManagerDataSource implements DataSource -> JdbcTemplate (CRUD)
-> driverClassName
-> url
-> username
-> password

Jdbc Template method->


-> update( ) -> insert , delete , update
-> execute( ) -> select

***********************************************************************************

bean template for jdbc-


<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
name="ds">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/springjdbc"/>
<property name="username" value="root"/>
<property name="password" value="Himanshu@2001"/>
</bean>
<bean class="org.springframework.jdbc.core.jdbcTemplate" name="jdbcTemplate"
p:dataSource-ref="ds"/>

***********************************************************************************
**

selecting data using spring jdbc


-> for only one row
public T queryForObject(String sql, RowMapper<T> rowMapper, Object orgs)
public List<T> T query( .....)

**********************************************************************************

spring jdbc without using XML using javaConfig file


@Bean( "ds" )
public DataSource getDataSource() {

DriverManagerDataSource ds = new DriverManagerDataSource();


ds . setDriverC1assName ( " com.mysql . jdbc . Driver" ) ;
ds. setUr1 ( " j dbc : mysql : / / localhost : 3306/ springij dbc " ) ;
ds. setUsername ( " root " ) ;
ds. setPassword ( " root " ) ;
return ds;
}
@Bean( " jdbcTemp1ate " )
public JdbcTemp1ate get Template() {
new JdbcTemp1ate();
JdbcTemp1ate jdbcTemp1ate
jdbcTemp1ate. setDataSource(getDataSource( ) ) ;
return jdbcTemp1ate;
}

***********************************************************************************
**********

You might also like