Spring Cocenpts
Spring Cocenpts
========
SPRING 1:-non-invasive:
means it doesn�t force a programmer to extend or implement their class from any
predefined class or interface given by Spring API,.
Q: Loose Coupling ?
ans:-
Loose Coupling-
ex:
Interface Vehicle
{
void move();
}
class Traveler
{
Vehicle v;
public void setV(Vehicle v)
{
this.v = v;
}
void startJourney()
{
v.move();
}
}
spring container will inject either Car object or Bike object into the Traveler by
calling setter method, So if Car object is replaced with Bike then no changes are
required in Traveler class, this means there is loose coupling between Traveler and
Vehicle object.
Tight coupling EX:
------------------
class Traveler
{
Car c=new Car();
void startJourney()
{
c.move();
}
}
class Car
{
void move()
{
// logic...
}
In the above example, Traveler object is depends on car object. So traveler class
creating an object of Car class inside it [ line number 3 ]
If the other class object is created in the dependent class [ like Car class object
in Traveler class ], there exist tight coupling, i mean if method in car object is
changed then we need to do the changes in the Traveler class too so its the tight
coupling between Traveler and Car class objects
--------------
Note 1:-to perform setter or constructor injection by reading the information from
an external file called spring configuration file
Note 2:-In case of setter injection, the class must contain a setter method to get
dependencies other wise spring container doesn�t inject the dependencies to
dependent object
Note 3:-Actually this configuration file contains all bean configurations about our
application
Next step is to bring spring IOC container into our program, it will be created by
reading configuration xml through Resource object. Spring IOC container is called
BeanFactory and this container is responsible for creating the bean objects and for
injecting it�s dependencies through out our applications.
BeanFactory is the interface and XmlBeanFactory is an implementation class of it,
BeanFactory given in org.springframework.beans.factory. * and XmlBeanFactory is
given in org.springframework.beans.factory.xml.* pack
<ref parent="someBeanId"> should be used in the child config file to reference the
parent id.
<ref bean="someBeanId"> should be used when you do not have a duplicate id in your
parent-child config files.
Note 7 :-if both constructor and setter injection applied for same property then
constructor injection will be overridden by setter injection, because constructor
injection will happen at the object creation time,
1-Setter injection
Setter Injection, Dependency In The Form Of Collections-
Set
List
Map
Properties
<bean id="id1">
<property name="sb" class="DemoBean"> // parent object
<ref local="id2" /> // ref obj in parent and local mean search in same xml
</property>
</bean>
<property name="v">
<ref parent="id2" /> // understand again and do practicle
</property>
</bean>
</bean>
</list>
</property>
</bean>
</set>
</property>
</bean>
Setter Injection:
-----------------
1-> partial injection of dependencies can possible, means if we have 3 dependencies
like int, string, long, then its not necessary to inject all values if we use
setter injection. If you are not inject it will takes default values for those
primitives
2->Setter Injection will overrides the constructor injection value,
3->. If we have more dependencies for example 15 to 20 are there in our bean class
then, in this case setter injection is not recommended as we need to write almost
20 setters right, bean length will increase.
Constructor Injection:
----------------------
If autowiring is enabled then spring container will take care about injecting the
dependencies, programmer no need to configure into an xml file explicitly.
Autowiring is only supported if the dependancies are in the form of objects only,
to enable autowiring, we should add autowire attribute to the bean element [or]
bean tag,
EX:- MyClass obj
then MyClass is type and obj is the name of property.
Note:-
1) The default mode in traditional XML based configuration is no.
2) The default mode in java based @Autowired is byType.
Spring framework uses DataSource interface to obtain the connection with database
internally, i mean we will use any one of the following 2 implementation classes of
DataSource interface.
Org.springframework.jdbc.datasource.DriverManagerDataSource [ class ]
Org.apache.commons.dbcp.BasicDataSource [ class ]
execute()
update()
query()
Note: execute and update methods are for non-select operations on the database, and
query method is for select operations on the database.
</bean>
<bean>
</bean>
<bean>
<mvc:annotation-driven />
<mvc:annotation-driven />
===========================================================================http://
www.java2novice.com=============================================================
<constructor-arg type="int">
<value>120</value>
</constructor-arg>
@Bean(name="myColorBean")
public MyColor getMyColors(){
return new RedColor();
}
}
Q - How to read property file in spring using xml based configuration file ?
----------------------------------------------------------------------------
ans:-
using xml based configuration file
----------------------------------
Declare your property file in your xml based configuration file using
"context:property-placeholder" tag, and refer property key any where in the xml
based configuration file using ${db.host.url} syntax.
@Configuration
@PropertySource("classpath:/db.properties")
public class MyApplicationConfig {
@Autowired
Environment env;
@Bean(name="dbConfig")
public MyDbConfig getDbConfig(){
Note:-
Spring allows you to restrict parent bean by specifying abstract property. Below
configuration makes the parent bean abstract, so you cannot creat object for parent
bean
@Required
public void setOrder(Order ord){
this.order = ord;
}
}
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@Repository: You need to use this annotation with in the persistance layer, which
acts like database repository.
byName: Autowiring by property name. This option will inspect the container and
look for a bean named exactly the same as the property which needs to be autowired.
For example, if you have a bean definition which is set to autowire by name, and it
contains a master property (that is, it has a setMaster(..) method), Spring will
look for a bean definition named master, and use it to set the property.
@Autowired(required=false)
public PaymentGateway(Order ord){
this.order = ord;
}
To solve this issue, use @Qualifier annotation with bean name, so spring container
picks the right bean declaration and assigned to the property.