Spring Short Notes
Spring Short Notes
=====================================
- Every class we are writing in spring application can be called as Spring Bean.
(15) What will be done by the Spring Container when it finds following bean
definition?
Ans:
<bean id="aobj" class="com.jkindia.spring.A "> .
<property name="a" value="99" />
<property name="msg" value="Hello Guys"/>
</bean>
A aobj=bew AO;
aobj.setA(99);
aobj.setMsg("HelloGuys");
(16) What will be done by the Spring Container when it finds following bean
definition?
<bean id="bobj" class="com.jlcindia.spring.B">
<constructor-arg value="88" />
<constructor-arg value="Hai Guys"/>
</bean>
(17) What will be done by the Spring Container when it finds following bean
defination?
Ans: Spring Configuration file is an XML file which contains various bean
definitions.
Ans: Yes.
<beans>
<bean id="hello1" class="com.Hello"/>
<bean id="hello2" class="com.Hello"/>
</beans>
Hello h1=(Hello)ctx.getBean("hello1");
Hello h2=(Hello)ctx.getBean("hello2");
Here h1!=h2
<beans>
<bean id="hello" class="com.Hello">
<constructor-arg value="99"/>
</bean>
</beans>
class Hello{
int x;
// No constructor
}
class Hello{
int x;
//No setter
}
Ans: Bean Instance created by Spring Container can be in one of the following
Scopes.
(a) singleton (b) prototype (c) request (d) session (e) global-session
• Usage:
With XML Config:
<bean id="" class="" scope=""/>
Ans: singleton:
------------
• When bean scope is singleton then only one instance will be created for that bean
and the same instance will be returned when you call getBean() method.
• singleton is the default scope in the ApplicationContext container.
• When scope is single-ton then default loading type is aggressive loading.
prototype
---------
• When bean scope is prototype then every time a new instance will be created for
that bean when you call getBeanO method.
• When scope is prototype then default loading type is lazy loading.
Usage :
With XML Config :
<bean id="" class="" scope="" Jazy-init=""/>
(29) What is the difference between Lazy loading and Aggressive loading ?
Ans:
(a) Aggressive loading or Eager loading
----------------------------------------------
In the case of aggressive loading, all the Beans will be loaded, instantiated and
initialized by the container at the container start-up.
<bean id="" class="" scope="" Jazy-init="false"/>
In the case of lazy loading, all the Beans will be loaded, instantiated and
initialized
when you or container try to use them by calling getBean() method.
<bean id="" class="" scope="" lazy-init="true" />
Ans:
All 3 beans will be loaded at container start-up and all are singletons.
Order of loading: A-B-Hello
Ans:
No bean will be loaded at container start-up.
All are Singleton
Loads when you call getBean() method
Order of loading: B, Hello, A
Ans:
All 3 Beans will be loaded at container start-up.
All are Singletons
Loads when you call getBean() method
Order of loading: B, Hello, A
Ans:
2 beans will be loaded at container start-up and all are singletons.
Ans:
No Beans will be loaded at container start-up.
A,B are prototype.
Hello is singleton.
Ans:
All 3 Beans will be loaded at container start-up.
A,B are prototype.
Hello is singleton.
Order of loading: B, Hello, A
In the case of explicit wiring you have to specify the bean Dependencies
explicitly the container will inject those Dependencies.
<beans>
<bean id="ao" class="A"/>
<bean id="bo" class="B"/>
In the case of Auto wiring Spring Container can detect the Bean Dependencies
automatically and injects those Dependencies.
<beans>
<bean id="ao" class="A"/>
<bean id="bo" class="B"/>
<bean id="hello" class="Hello" autowire="xxx"/>
</beans>
(a1) byName
-------------
When autowire attribute value is byName then Spring Container checks whether
any bean instance running in the Container whose name(or id) is same as bean
property(variable) name or not
When bean is found with the matching name then it will be injected otherwise bean
property remains uninjected.
(a2) byType
---------
When autowire attribute value is byType then Spring Container checks
whether any bean instance running in the container whose Type is same
as bean property data Type or not.
(a3) constructor :
--------------------
(59) Annotations are replacement for XML but still we are writing Beans
in xml only. Can i use only annotations without xml ?
Ans: Yes, You can configure your Beans with Java Based Configuration without XML
Configuration.
Use the following Annotations for Java Based Configuration
@Configuration
@Bean
Ans: @Scope indicates the name of a scope to use for the instance returned from the
method.
Ans: When you want to use the Annotations, You need to do the following.
(a) You must enable context namesp:ace
(b) Add <context:annotation-config/>
class Hai {}
class A{}
class Hello{
@Autowired
A aobj;
@Autowired
Hai hai;
Ans: No.
Ans: Setter Injection or Field Injection -> Make the beans loosly coupled
Constructor Injection -> Make the beans tightly coupled
(71) How can i resolve Compile time polymorphism with constructor Injection ?
Ans: No
(86) You can Inject the required resources with D.I ? What is the use of
Initialization callbacks?
Ans: Mainly to check whether resources are initialized by the Spring Container or
not.
class Hello{
@Autowired
Hai hai;
@PostConstruct
public void initO{
if(hai==null){
hai= ..... ;
} else {
throw Some Exception;
}
}
void showO{
hai.m1();
}
}
(89) How can I get the reference of BeanFactory container into bean?
Ans: There are two ways to initialize your bean with BeanFactory reference.
(A) By implementing BeanFactoryAware interface and overriding
setBeanFactory() method.
(B) By using @Autowired (use this)
Ans: There are two ways to initialize your bean with ApplicationContext
reference.
You want to make jlc() method as lifecycle method for every bean
-----------------------------------------------------------------
(93) What are the difference between BeanFactory and ApplicationContext Container?
(94) We are creating Spring container object in main method after scope over still
container
objects exits. How to justify ( Every object should be inside scope).
Ans: No
Ans: No No No
(101) Can I inject the Bean with One scope into another Bean with different scope?
Ans: Yes
(104) Can I inject the Bean defined one xml into another bean defined in another
xml?
Ans: Yes
Ans:
Ans:
Ans:
Ans:
Ans:
Ans:
Ans:
Ans: (a) Singleton Pattern (b) Factory Pattern (c) Decorator Pattern
(114) While you are configuring the beans in xml, we are hard-coding the values?
Can I make
it dynamic?
Ans: Yes, You can use property files.( using Externalizing Bean properties)
Ans:
Ans: Yes
(121) What is the Use of Property Editors?
Ans:
Ans:
getMessageQ
publishEvent()
etc
Ans:
Ans:
Ans:
Ans:
(132) What is PointCut? How to define PointCuts in Spring API based AOP ?
Ans:
Ans:
(l34) Write the AspectJ pointcut Expression for invoking any
any method from any business service ?
(135) Write the AspectJ point Expression for invoking the methods starting with add
from any business service?
(l36) What is an Advisor? How to define Advisor in Spring API Based AOP?
Ans :
(l37) What is the difference between Target Object and Proxy Object?
Ans :
Ans:
Ans :
(141) What properties has to be configured with ProxyFactoryBean when you are using
Class Model?
Ans : You need to configure the following three properties:
1 )targetClass
2)target
3)interceptorNames
(142) What properties has to be configured with ProxyFactoryBean when you are using
interface Model?
Ans:
Ans:
Ans : You must enable aop namespace to use the following tags.
<aop:config> <aop:aspect> <aop:pointcut>
<aop:before> <aop:after-returning> <aop:after-throwing>
<aop:after> <aop:around>
Ans:
===================================================================================
=================================================