SPRING MyNote-All
SPRING MyNote-All
- The basic concept of IOC (Dependency of Injection) is that you do not create your
objects but describe how they should be
created.
describe which services are needed by which components in a configuration file.
2. Setter Injection :
- Dependencies are assigned through setter method.
in configuration file :
The Code:
First Create subclass of StoredProcedure: MyStoredProcedure
super(jdbcTemplate, name);
setFunction(false);
}
}
Use MyStoredProcedure to call database stored procedure:
myStoredProcedure.setParameters(paramArray);
myStoredProcedure.compile();
Mainly it is the scope of a beans which defines their existence on the application
Singleton : It means single bean definition to a single object instance per Spring
IOC container.
Prototype : It means a single bean definition to any number of object instances.
- Auto-wiring beans with xml configuration: In Spring framework, you can wire beans
automatically with auto-wiring feature. To enable auto-wiring just define the
�autowire� attribute in <bean> tag.
2. byName � Auto wiring by property name. If the name of a bean is same as the name
of other bean property, auto wire it.
- In below example the name of the person bean is same as name of �customer� bean�s
property Person object. So spring will auto-wire it via setter method.
<bean id="customer" class="com.test.autowire.Customer" autowire="byName"/>
<bean id="person" class="com.test.autowire.Person" />
3. byType � Auto wiring by property data type. If data type of a bean is compatible
with the data type of other bean property, auto wire it.
- In below example the data type of the person bean is same as name of �customer�
bean�s property Person object. So spring will auto-wire it via setter method.
<bean id="customer" class="com.test.autowire.Customer" autowire="byType"/>
<bean id="person" class="com.test.autowire.Person" />
- Here the data type of �person� bean is same as the constructor argument data type
in �customer� bean�s property (Person object), so, Spring auto wired it via
constructor method � �public Customer(Person person)�
<bean id="customer" class="com.test.autowire.Customer" autowire="constructor"/>
<bean id="person" class="com.test.autowire.Person" />
The Front Controller is basically a type of Design pattern which are being
implemented in different framework (e.g. Struts and Spring MVC etc.).
- In Spring MVC DispatcherServlet act as a Front Controller for the framework and
responsible for intercepting every request and then dispatches/forwards request to
appropriate controller. Configure the DispatcherServlet in the web.xml file of web
application and request which we want to be handled by DispatcherServlet should be
mapped using URL mapping.
no: This is default setting. Explicit bean reference should be used for wiring.
byName: When autowiring byName, the Spring container looks at the properties of the
beans on which autowire attribute is set to byName
in the XML configuration file. It then tries to match and wire its properties with
the beans defined by the same names in the configuration file.
byType: When autowiring by datatype, the Spring container looks at the properties
of the beans on which autowire attribute is set to
byType in the XML configuration file. It then tries to match and wire a property if
its type matches with exactly one of the beans name in configuration file. If more
than one such beans exist, a fatal exception is thrown.
=>Primitive data types: You cannot autowire simple properties such as primitives,
Strings, and Classes.
=>Confusing nature: Autowiring is less exact than explicit wiring, so if possible
prefer using explicit wiring.
<context:annotation-config/>
Q @Required annotation
This annotation simply indicates that the affected bean property must be populated
at configuration time, through an explicit property value in
a bean definition or through autowiring. The container throws
BeanInitializationException
if the affected bean property has not been populated.
40. @Autowired annotation
The
@Autowired
annotation provides more fine-grained control over where and how autowiring should
be accomplished. It can be used to autowire bean on
the setter method just like
@Required
annotation, on the constructor, on a property or pn methods with arbitrary names
and/or multiple arguments.
Q @Qualifier annotation
When there are more than one beans of the same type and only one is needed to be
wired with a property,
the @Qualifier annotation is used along with @Autowired annotation to remove the
confusion by specifying which exact bean will be wired.
66. WebApplicationContext
The
WebApplicationContext
is an extension of the plain
ApplicationContext
that has some extra features necessary for web applications. It differs from a
normal
ApplicationContext
in that it is capable of resolving themes, and that it knows which servlet it is
associated with.
<artifactId>jackson-databind</artifactId>
<version>${jackson.databind-version}</version>
</dependency>
33. What are some of the important Spring annotations you have
used?
@ResponseBody � for sending Object as response, usually for sending XML or JSON
data as
response.
@PathVariable � for mapping dynamic values from the URI to handler method
arguments.
@Autowired � for autowiring dependencies in spring beans.
@Quali??er � with @Autowired annotation to avoid confusion when multiple instances
of bean
type is present.
@Service � for service classes.
@Scope � for con??guring scope of the spring bean.
@Con??guration, @ComponentScan and @Bean � for java based con??gurations.
<context:annotation-config/> element.
26. What�s the difference between @Component, @Controller, @Repository & @Service
annotations in Spring?
@Component: This marks a java class as a bean. It is a generic stereotype for any
Spring-managed component. The component-scanning
mechanism of spring now can pick it up and pull it into the application context.
@Controller: This marks a class as a Spring Web MVC controller. Beans marked with
it are automatically imported into the Dependency Injection container.
@Service: This annotation is a specialization of the component annotation. It
doesn�t provide any additional behavior over the @Component
annotation. You can use @Service over @Component in service-layer classes as it
specifies intent in a better way.
@Repository: This annotation is a specialization of the @Component annotation with
similar use and functionality. It provides additional benefits
specifically for DAOs. It imports the DAOs into the DI container and makes the
unchecked exceptions eligible for translation into Spring
DataAccessException.