Showing posts with label Annotations. Show all posts
Showing posts with label Annotations. Show all posts
Friday, June 2, 2017
Java Experience latest Interview Quetions
1. Write the program to reverse the string without modify the special character?
2. Write the program to compare the object using comparable interface?
3. Write the program for singleton?
4. Explain concurrent packages?
5. Difference between @inject and @service annotation
6. Advantages of microservice and cassendra?
7. Write the program for executor library?
8. Whats inner join?
9. Write the program for rest controller?
10. Can we have multiple session factory in Hibernate?
11. Explain IOC container and dependency injection?
12. How many test cases you will write to test a method?
Tuesday, March 7, 2017
Happient mind Bangalore Telphonic interview Quetions
1.Restful API Client side code?
2.What is weaving?
3.Transaction management in Spring?
4.Spring and hibernate Integration?
5.Java 8 Features?
a.Lamda-Expression
b.Method References
c.Functional Interfaces
d.Streams
e.Optional Class
f.Date API
g.Base64
2.What is weaving?
3.Transaction management in Spring?
4.Spring and hibernate Integration?
5.Java 8 Features?
a.Lamda-Expression
b.Method References
c.Functional Interfaces
d.Streams
e.Optional Class
f.Date API
g.Base64
Monday, January 5, 2015
Migrate the xml to a @Configuration
@Configuration Implement in Spring
Migrate the xml to a
@Configuration
in a few steps:- Create a
@Configuration
annotated class:@Configuration public class MyApplicationContext { }
- For each
<bean>
tag create a method annotated with@Bean
:@Configuration public class MyApplicationContext { @Bean(name = "someBean") public SomeClass getSomeClass() { return new SomeClassImpl(someInterestingProperty); // We still need to inject someInterestingProperty } @Bean(name = "anotherBean") pubic AnotherClass getAnotherClass() { return new AnotherClassImpl(getSomeClass(), beanFromSomewhereElse); // We still need to inject beanFromSomewhereElse } }
- In order to import
beanFromSomewhereElse
we need to import it's definition. It can be defined in an XML and the we'll use@ImportResource
:@ImportResource("another-application-context.xml") @Configuration public class MyApplicationContext { ... }
If the bean is defined in another@Configuration
class we can use the@Import
annotation:@Import(OtherConfiguration.class) @Configuration public class MyApplicationContext { ... }
- After we imported other XMLs or
@Configuration
classes, we can use the beans they declare in our context by declaring a private member to the@Configuration
class as follows:@Autowired @Qualifier(value = "beanFromSomewhereElse") private final StrangeBean beanFromSomewhereElse;
Or use it directly as parameter in the method which defines the bean that depends on thisbeanFromSomewhereElse
using@Qualifier
as follows:@Bean(name = "anotherBean") pubic AnotherClass getAnotherClass(@Qualifier (value = "beanFromSomewhereElse") final StrangeBean beanFromSomewhereElse) { return new AnotherClassImpl(getSomeClass(), beanFromSomewhereElse); }
- Importing properties is very similar to importing bean from another xml or
@Configuration
class. Instead of using@Qualifier
we'll use@Value
with properties as follows:@Autowired @Value("${some.interesting.property}") private final String someInterestingProperty;
This can be used with SpEL expressions as well. - In order to allow spring to treat such classes as beans containers we need to mark this in our main xml by putting this tag in the context:
<context:annotation-config/>
You can now import@Configuration
classes exactly the same as you would create a simple bean:<bean class="some.package.MyApplicationContext"/>
There are ways to avoid spring XMLs altogether but they are not in the scope of this answer. You can find out one of these options in the blog post on which I'm basing my answer (disclaimer - the post is written by me).
The advantages and disadvantages of using this method
Basically I find this method of declaring beans much more comfortable than using XMLs due to a few advantages I see:
- Typos -
@Configuration
classes are compiled and typos just won't allow compilations - Fail fast (compile time) - If you forget to inject a bean you'll fail on compile time and not on run-time as with XMLs
- Easier to navigate in IDE - between constructors of beans to understand the dependency tree.
- Possible to easily debug configuration startup
The disadvantages are not many as I see them but there are a few which I could think of:
- Abuse - Code is easier to abuse than XMLs
- With XMLs you can defined dependencies based on classes that are not available during compile time but are provided during run-time. With
@Configuration
classes you must have the classes available at compile time. Usually that's not an issue, but there are cases it may be.
Bottom line: It is perfectly fine to combine XMLs,
@Configuration
and annotations in your application context. Spring doesn't care about the method a bean was declared with.
Subscribe to:
Posts (Atom)