Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
*****
1) spring core
2) AOP
4) WEB
5) TEST
6) Instrument
1) Spring core - majorly about Dependency injection and functionalities on which other modules rely on.
2) AOP - cross cutting concerns of applications are called aspects. ( eg security, logging, transaction
mgmt )
supports JDBC, ORM (Object Relational Mapping), OXM (object XML Mapping) , JMS (Java Message
Service)
ORM - supports Hibernate, iBATIS ( apache discontinued on 2010 ), JDO (java Data Objects) and JPA
(Java Persistence API)
OXM (object XML Mapping) - XML mapping supported for Castor, JAXB (Java Arch for XML Binding), JiBX,
XMLBeans, XStream.
4) WEB
supports and integrates with other single (web ) tier frameworks like Structs, JavaServerFaces, Velocity,
FreeeMaker, JavaServer Pages.
5) TEST.
DI
**
**
public AccountService() {}
public void setAccountDao(AccountDao accountDao)
this.accountDao = accountDao;
import javax.sql.DataSource;
import com.springinpractice.ch01.dao.AccountDao;
public JdbcAccountDao() {}
this.dataSource = dataSource;
<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-3.1.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
</bean>
</bean>
</bean>
</beans>
wrong ways:
But this way will provide you AccountService --> AccountDAO (interface ) __ BasicDataSource (concrete
relationship). DAO and BasicDataSource got coupled.
So in a way the tester can mock up with other datasources only by editing the touching the code.
you can say you will do by having interface DataSource and using a config file.
But in that way also the properties needed and parsing of config creates configuration coupling between
the DAO and datasource as the class will have congifuration
AccountService --> AccountDAO (interface ) ---------configures----------> (Basic not sure of it) DataSource
Sample PROJECT
**************
step1)
define domain objects
Note: we need to write setters explicitly (avoided in spring boot by annotation , spring will use these
setters for DI, setter DI. JavaBeans programming model suggested the setters first , spring uses it).
Concrete DAO will instantiate Domain objects from the raw data it got from Data source through the
datasource interface exposed methods.
step 4) service will use the domain objects and code business logic.
application-Context.xml can be broken down into multiple parts. DAO configs in one file etc.
***********************
***********************
Spring ships with different schemas for configuring its different pieces of functionality, such as AOP and
transaction management.
<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-3.1.xsd">
and properties will be having name and value/ref attribute.( for classes use ref and for resources use
value )
parts of that
import org.springframework.context.ApplicationContext;
The ApplicationContext interface and its implementations are the gateway into the beans through
Spring.
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.springinpractice.ch01.model.Account;
import com.springinpractice.ch01.service.AccountService;
ApplicationContext appCtx =
new
ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService =
(AccountService)appCtx.getBean("accountService");
.findDeliquentAccounts();
System.out.println(a.getAccountNo());
************************
************************
<xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
The beans namespace is the most fundamental and deals with DI;
it provides a way to define beans and wire dependency relationships between them. To create a
configuration file that uses the beans namespace, you create an XML document and reference
the schema:
*****************************************************
<constructor-arg ref="accountDao"/>
</bean>
this.accountDao = accountDao;
NOTE: Property values are set to strings. If the parameters has different type, spring will convert to the
type needed.
PropertyPlaceholderConfigurer
*****************************
declare it as a bean
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
</bean>
</bean>
And the config file looks like one below ( usually put up at src/main/resources, and since that is included
in classpath, will be loaded without errors)
springbook.properties):
dataSourcefile.driverClassName=com.mysql.jdbc.Driver
dataSourcefile.url=jdbc:mysql://localhost:3306/springbook?autoReconnect=true
dataSourcefile.username=root
dataSourcefile.password=secret
******************
define how the instiantiation and destruction of beans should happen as requested from containers.
5 ways:
scope="singleton|prototype|request|session|globalSession"/>
***************
***************
not equivalent to singleton design pattern. where one class for a classloader.
Declaring a singleton bean in Spring ensures that only one instance exists on a per-container basis
As long as resources are thread-safe—meaning they’re synchronized, immutable, have no state, or have
fields where any of the previous criteria are strictly met
—you can safely declare them with singleton scope to eliminate the overhead of creating them each
time they’re requested.
For example, singleton (default ) services often have references to singleton (defualut)DAOs, and the
DAOs might
PROTOTYPE SCOPE
***************
***************
Prototype-scoped beans are created every time they’re requested via ApplicationContext’s getBean()
method, or each time they’re injected into other beans.
The last three scopes—request, session and global session—are useful only in the context of web
applications.
( have additional steps if you are using frameworks other than spring MVC)
Request-scoped beans are created each time an HTTP request makes its way into a servlet resource that
is injected with one.
it’s restricted to one thread at a time, which is tied to the current session of the client making the
requests.
they exist throughout an entire session. created and managed for each individual portlet.
Global session–scoped beans are applicable only in the context of portlet applications.
they exist throughout an entire session, but they’re shared among all the portlets in a complete portlet
web application,
Additional steps
****************
****************
****************
1) register a Request-
...
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
...
</web-app>
These listeners and filters enable the required integration between Spring and whatever servlet
container you’re using.
Spring can intercept requests and decorate them with this scoping functionality.
beans with request, session and global session scope should be intialized from Spring context that are
aware of web.
(XmlWebApplicationContext)
1.4.3 P namespace ( bean namespace is define in xsd. p namespace dont have XSD. It is implemented in
spring)
*****************
*****************
p namespace extends bean namespace. It uses some XML syntax for application-context.xml
configuration to be consise.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
</bean>
EG2:
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${dataSource.driverClassName}"
p:url="${dataSource.url}"
p:username="${dataSource.username}"
p:password="${dataSource.password}"
/>
******************
******************
intended to improve constructor injection. also supports -ref like p namespace.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:c="http://www.springframework.org/schema/c"
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-3.1.xsd">
<constructor-arg value="Joshua"/>
<constructor-arg value="White"/>
</bean>
c:firstName="Joshua"
c:lastName="White"/>
c:_0="Willie"
c:_1="Wheeler"/>
*************************************
1.5.1 @AutoWired
****************
****************
import org.springframework.beans.factory.annotation.Autowired;
... other imports omitted ...
@Autowired
...
The @Autowired annotation can be applied to constructors or fields and allows you to wire relationships
When a field is @Autowired, Spring looks through the container for beans with a matching type to
inject.
@Autowired
step2 ) configure the application-context.xml - declare the annotation-config element and enable it
(<context:annotation-config/>)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:annotation-config/>
<bean id="accountService"class="com.springinpractice.ch01.service.AccountService"
p:accountDao-ref="accountDao"/>
NOTE: If you want to use some mimatch named class to be wired, use qualifier.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@Autowired
@Qualifier("hibernateAccountDao")
****************************
*****************************
import org.springframework.stereotype.Service;
Still we declare the beans in application-context.xml. We can avoid that too like we avoided property
elements.
1) Spring’s stereotype annotations, you can annotate classes and 1) enable component scanning
(<context:component-scan base-package="com.springinpractice.ch01"/>)
and pull it into the application context. (generic for all beans)
@Repository - for DAO ( with feautres transferring exceptions they throw into Springs
DataAccessExceptions)
@Controller - for spring web MVC controller components with compilimentary annotation
@RequestMapping, to map URLs to instance methods of a class.
************************
************************
provide the base package from where the bean/components has to scanned recurssively.
<context:component-scan base-package="org.springinpractice.ch01"/>
Bad news, if you use third party classes from jars, and cant add annotations you have to declare
them in application-context.xml.
So naming of the beans created by component scanning has the naming convention: (their
uncapitalized, nonqualified classnames)
you can implement Spring’s BeanNameGenerator interface to customize the naming strategy.
*************************************
*************************************
XML config:
multiple xml for DAO, service (each architectural slices) can be helpful.
Annotations:
seeing the java file that contains the classes ( domain objects or service) you will know the entire info of
these classes with annotations.