Spring 3 Certification Study Notes
Spring 3 Certification Study Notes
Container
Objective/text copy/pasted from
SpringSource study guide
General
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
beans.html
singleton
What are the main scopes that can be used Prototype new each time, init called, destroy not called, ignores lazy-init
alternatively?
session
request
singleton one per container
[also globalSession (portlets) and custom which I think are out of scope]
[web flow scopes which are out of scope]
Lifecycle
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
beans.html
What are
BeanFactoryPostProcessors and
BeanPostProcessors? When are they called
in the startup
process?
original bean
{What is a BeanFactory?}
Annotations
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
beans.html
Enabling component-scanning
@Autowired
@Qualifier(name) or @Resource (not Spring specific)
chooses specific bean when more than one exists of same type
Can also put on specific parameter
What is the role of the @PostConstruct and Equivalent to init-method and destroy-method
@PreDestroy annotations
Enabling the scanning of annotations such
as @Required and @PreDestroy
<context:annotation-config />
or <context:component-scan /> which includes it
@Required goes on setters, not fields
@PreDestroy goes on methods
Miscellaneous
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
beans.html
classpath:
file:
http:
How
new ClasspathXmlApplicationContext(/com/mine/app-config.xml);
to refer to a Spring configuration file inside a
package.
The different implementations of
ApplicationContext that can be used in an
application.
Extract common bean setup into one super bean to reduce duplicate setup
code
<bean id=parent class=Mine abstract=true />
<bean id=child parent=parent class=Mine/>
A bean without a class or factory is implicitly abstract.
id must be unique
name can specify multiple names separated by comma, semicolon or space,
allows more characters in name such as slashes
If need to inject a bean with request/session scope into a bean with singleton
scope, the proxy intercepts it and makes sure you get the right bean for each
call.
Class name in bean.xml is that of the factory, setters called on the factory rather
than created bean, but bean id maps to type returned by factory's getObject()
{Spring EL}
#{systemProperties.name}
JavaConfig
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
beans.html
annotations
Testing
http://static.springsource.org/spring/docs/3.0.x/spring-frameworkreference/html/testing.html
@RunWith(SpringJUnit4ClassRunner)
@ContextConfiguration(config.xml)
@ContextConfiguration(locations={config.xml})
Defaults to testclassname-context.xml
The context is cached across tests/test classes that use same config files unless
you use @DirtiesContext.
Unit test tests one class in isolation with mocks. Integration test tests multiple
classes together. Only integration tests interact with Spring.
AOP
Recommendations
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
aop.html
Advice
Code to execute
Pointcut
Joinpoint
Place in code where code can be injected. In Spring this is just before/after
methods.
Pointcuts
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
aop.html
Advices
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
aop.html
Different kinds of Advice. How do they differ @Before before call code aborts if throw exception
from each other?
@After after unconditionally
@AfterReturning after on success takes JoinPoint and return value, can
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
aop.html
<aop:aspectj-autoproxy />
Yes.
<aop:config>
<aop:aspect ref=adviceBean>
<aop:before pointcut=execution.... />
</aop:config>
Proxies
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
aop.html
They have the same public interface if there is an interface. You must have an
interface or non-final class to create a proxy.
Can only be used for methods, limited pointcut syntax, doesn't get called if caller
and callee are methods in same class
General
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
dao.html
The JdbcTemplate
http://static.springsource.org/spring/docs/3.0.x/spring-frameworkreference/html/jdbc.html
With regard to result set parsing [when need RowMapper<A> mapper = new RowMapper<A>() {
to map to custom object]
public A mapRow(ResultSet rs, int row) throws SQLException {} };
jdbc.query(sql, mapper); - for list
jdbc.queryForObject(sql, mapper); - for single row
With regard to result set parsing [when need RowCallbackHandler handler = new RowCallbackHandler() {
to write out data to file but not return it]
public void processRow(ResultSet rs) {}
};
jdbc.query(sql, handler);
With regard to result set parsing [merging
rows into a list or other return object]
};
jdbc.query(sql, extractor);
With regard to querying [for
insert/update/delete row]
jdbc.update(sql);
jdbc.execute(sql);
Hibernate
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
orm.html
Configuration of a SessionFactoryBean in
xml [listing each entity]
<bean id=f
class=os.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
<property name=dataSource ref=d />
<property name=annotatedClasses>
<list><value>Class1</value><value<Class2</value></list>
</bean>
Configuration of a SessionFactoryBean in
xml [scanning for annotations]
<bean id=f
class=os.orm.hibernate3.annotation.AnnotationSessionFactoryBean>
<property name=dataSource ref=d />
<property name=packagesToScan>
<list><value>com/mine/*/entity</value></list>
</bean>
Configuration of a SessionFactoryBean in
xml [listing each hbm file]
<list><value>classpath:/package/hbm.xml</value></list>
</bean>
{What are benefits of transactions for
Hibernate?}
Transactions
http://static.springsource.org/spring/docs/3.0.x/spring-frameworkreference/html/transaction.html
<bean id="mgr"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
HibernateTransactionManager supports Hibernate and JDBC
JdbcTransactionManager only supports JDBC
<bean id="mgr"
class="org.springframework.transaction.jta.JtaTransactionManager" />
<aop:config>
<aop:advisor pointcut-ref=p advice-ref=ta />
</aop:config>
<tx:advice id=ta>
<tx:attributes>
<tx:method name=update* timeout=60 />
<tx:method name=* timeout=30 read-only=true />
</tx:attributes>
</tx:advice>
Can it be
overridden?
General configuration
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
mvc.html
View Resolver Maps returned view name to view implementation. Can also
return null/void (to use default view) or a concrete class such as new
JstlView(path)
Handler Mapping Identifies correct controller to call. Spring 3 uses the default
one which goes by the @RequestMapping annotation defined in the @Controller
Dispatcher Servlet front controller delegating to web infrastructure beans
(handler mappings, view resolvers, type converters) and calling controllers/views
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)
Controllers
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/
mvc.html
Bootstrapping a root WebApplicationContext The DispatcherServlet takes a contextConfigLocation parameter in the web.xml
using the ContextLoaderListener
or uses the default of name-servlet.xml
Also need to define listener so loads the root ApplicationContext before
initializing the servlet.
<listener><listenerclass>org.springframework.web.context.ContextLoaderListener</listenerclass></listener>
General usage of the @Controller
annotations
Takes value of path of URL to match on method level and optionally class level
adding slashes as needed. Combined, they form the absolute URL path
including Ant style wildcards. Can also pass method=RequestMethod.GET (or
the like) to restrict by type or filter by params/headers.
The view name. Or more specifically the name passed to the view resolver to
determine the view name. For example, one can add a prefix/suffix to all strings
returned.
REST
http://static.springsource.org/spring/docs/3.0.x/spring-frameworkreference/html/remoting.html#rest-client-access
Send an HTTP response code with the response. If defined on the method,
sends that header. If defined on an exception, only sends if that error occurs.
For example, @ResponseStatus(HttpStatus.CREATED)
An empty body can be used for REST so no view is used.
Annotation can go on @RequestMapping method or an exception class.
Advanced topics
Objective/text copy/pasted from
SpringSource study guide
Remoting
http://static.springsource.org/spring/docs/3.0.x/spring-frameworkreference/html/remoting.html
Advantages of using Spring Remoting rather Hide plumbing/complexity, support multiple protocols, simplify things when both
than plain RMI?
ends of remote call run Spring
Goal of the RMI Service Exporter
Handle server side of RMI interaction bind to registry, expose endpoint (used
for everything except EJB; EJB already has mechanism to export remote)
<bean class=org.springframework.remoting.rmi.RmiServiceExporter>
and set properties:
serviceName (name in registry)
serviceInterface (interface classname)
service (reference to POJO implementing serviceInterface)
</bean>
<bean class=org.springframework.remoting.rmi.RmiProxyFactoryBean>
and set properties:
serviceInterface (interface classname)
serviceUrl (rmi://server:port/serviceName)
</bean>
HttpInvoker uses HTTP POST while RMI one uses RMI. To use create bean
HttpInvokerServiceExporter with service (reference) and service interface (class
name). The serviceName is specified as the name of the bean because default
is to use BeanNameUrlHandlerMapping. Also create bean
HttpInvokerProxyFactoryBean with same properties as for RMI.
Needs web server on the server side, but not the client side.
Security
http://static.springsource.org/springsecurity/site/docs/3.0.x/reference/springsecurity.html
<security:http>
<security:intercept-url pattern=/stuff/**
[method=GET] access=role1, role2 />
Pattern uses Ant syntax (** is any level of directories), can change to reg exp
Access can get IS_AUTHENTICATED_FULLY or a list of roles
If more than on intercept-url, matched top to bottom
filters=none - no security applied
Pass allowed roles. @Secured is Spring's annotation and takes an array of roles
@RolesAllowed
<security:authentication-manager>
<security:authentication-provider>
Database: <security:jdbc-user-service data-source-ref=ds />
In memory: <security:user-service properties=a.properties />
hard coded in xml, LDAP, JAAS, SSO, JDBC, Open Id, etc
JMS
http://static.springsource.org/spring/docs/3.0.x/spring-frameworkreference/html/jms.html
Connection Factory
defaultDestination not required, but common
JMX
http://static.springsource.org/spring/docs/3.0.x/spring-frameworkreference/html/jmx.html
Yes, just register them as a bean in the XML config and Spring will notice they
are already MBeans. (these are classes implementing class ending in MBean)
<context:mbean-export /> turns on looking for MBeans
<bean id=mine class=ExistingMBean />