This is my ClientService class :
public interface ClientService{
public void saveClient(Client client);
}
This is the implementation :
#Service("clientService")
public class ClientServiceImpl{
#Autowired
private ClientRepository clientRepository;
public void saveClient(Client client) {
clientRepository.save(survey);
}}
This is my repository:
#Repository("clientRepository")
public interface ClientRepository extends JpaRepository<Client, Long> { }
And in my controller I have :
#Controller
public class LoginController {
#Autowired
private ClientService clientService;
which I then try to access
clientService.save in the function.
What annotation could I possibly be missing or what am I doing wrong ?
Cuz I've done this the same way before and it has worked for other services.
Thank you
Fixed it!
I had forgotten implements ClientService in the ClientServiceImplementation class!
Related
I have the following situation:
public interface ServiceAura extends Serializable { }
#Service
public class ServiceA implements ServiceAura {
....
}
#Service
public class ServiceB implements ServiceAura {
....
}
Now from the controller I need to call both of them by separate:
#Path("")
#Controller
public class ServiciosAuraPortalRESTfulService {
#Autowired
private ServiceAura srvA;
#Autowired
private ServiceAura srvB;
}
I have read about #Qualified, is this the only way? How can I archive this?
You're right. You can use #Qualifier("ServiceA") to specify which implementation you want to autowire.
#Path("")
#Controller
public class ServiciosAuraPortalRESTfulService {
#Autowired
#Qualifier("ServiceA")
private ServiceAura srvA;
#Autowired
#Qualifier("ServiceB")
private ServiceAura srvB;
}
On the service itself, you can use the annotation #Primary to specify which one is the default implementation that you want.
Alternatively, you can use the application context to retrieve a specific bean. You'll need to autowire the ApplicationContext class and then retrieve it with ServiceAura srvA = context.getBean(ServiceA.class);
There are two ways to do this.
The first way is using #Qualifier annotation as you've stated.
#Path("")
#Controller
public class ServiciosAuraPortalRESTfulService {
#Autowired
#Qualifier("serviceA")
private ServiceAura srvA;
#Autowired
#Qualifier("serviceB")
private ServiceAura srvB;
}
Your services should be defined like this:
#Service
#Qualifier("serviceA")
public class ServiceA implements ServiceAura {
....
}
#Service
#Qualifier("serviceB")
public class ServiceB implements ServiceAura {
....
}
Another way is to create interfaces that extend interface ServiceAura
interface ServiceAInterface extends ServiceAura {
}
class ServiceA implements ServiceAInterface {}
.... // the same for service B
And then in code:
public class ServiciosAuraPortalRESTfulService {
#Autowired
ServiceAInterface serviceA;
}
I followed the reference guide for creating and customizing Repositories and came up with the following:
public interface UserRepository extends JpaRepository<User, Long>, UserRepositoryCustom {
}
#Transactional(readOnly = true)
public class UserRepositoryCustomImpl implements UserRepositoryCustom {
#Override
public User findByToken(UUID token) {
return new User();
}
}
public interface UserRepositoryCustom {
User findByToken(UUID token);
}
In my case userRepository.findByToken(token);returns null.
#Edit
The test below fails
#RunWith(SpringRunner.class)
#DataJpaTest
#AutoConfigureTestDatabase(replace = NONE)
public class UserRepositoryTest {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
#Autowired
private TestEntityManager entityManager;
#Autowired
private UserRepository userRepository;
#Test
public void test() throws Exception{
assertNotNull(userRepository.findByToken(UUID.randomUUID()));
}
}
Your custom implementation is named wrong. It should be named after the class name of the Repository, not after the interface declaring the custom method.
Just renamed UserRepositoryCustomImpl to UserRepositoryImpl
The reason the method currently returns null is because Spring Data creates a query from the name and doesn't find a User with the specified token.
I am new to spring. I am attempting to autowire TestDAO without setter method. But i failed to autowire.
System.out.println("TestClass.testDAO "+testDAO); It returns null.
Kindly help me to unlock.
My xml config :
<context:component-scan base-package="com.test" />
<context:annotation-config/>
<bean id="testClass" class="com.test.TestClass" autowire="byName">
</bean>
Java classes :
#Component
public class TestClass {
#Autowired(required=true)
public TestDAO testDAO = null;
{
System.out.println("TestClass.testDAO "+testDAO);
}
}
#Repository
public class TestDAO{
}
Here's an example about how to fix your code:
#Component
public class TestClass {
#Autowired(required=true)
public TestDAO testDAO;
// When someone calls this method, the testDao component should
// be initialized with TestDAO instance.
public void someMethod(){
System.out.println("TestClass.testDAO "+testDAO);
}
}
public interface TestDAO extends JpaRepository<MyEntity, Long>{
}
Also, you could use the #Autowired annotation in a constructor.
#Component
public class TestClass {
public TestDAO testDAO;
#Autowired
public TestClass(TestDAO testDAO){
this.testDAO = testDAO;
System.out.println("TestClass.testDAO "+testDAO);
}
}
Hope it helps,
I'm working on one project with Spring 4.2.4.RELEASE.
I've heard about new features Spring 4 (especially about autowiring of generic types), and I was confused when the following code hadn't been compiled:
#Service
public interface AuthenticationService<T> { ... }
public class VKAuthenticationService implements AuthenticationService<VKToken> { ... }
#RestController
public class VKAuthenticationController {
#Autowired
private AuthenticationService<VKToken> service;
}
Thank you in advance for any assistance.
How about also declare #Service on your VKAuthenticationService
#Service(name="myService")
public class VKAuthenticationService implements AuthenticationService<VKToken> { ... }
and use #Autowired and #Qualifier to inject it
#RestController
public class VKAuthenticationController {
#Autowired
#Qualifier("myService")
private AuthenticationService<VKToken> service;
}
i wrote this simple example:
//file TestController.java
public interface TestController {
public List<Test> findAll();
}
//file TestControllerImp.java
#Controller
public class TestControllerImp implements TestController{
#Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory=sessionFactory;
}
public List<Test> findAll() {
return sessionFactory.getCurrentSession().createQuery("from Test").list();
}
}
//file TestService.java
#Service
public class TestService {
#Autowired
private TestController controller;
public boolean flag=true;
public void setController(TestController controller){
this.controller=controller;
}
#Transactional
public List<Test> useController(){
flag=false;
return controller.findAll();
}
}
And this is my try:
TestService s1=context.getBean(TestService.class);
TestService s2=context.getBean(TestService.class);
List<Test> list=s1.useController();
System.out.println(s1.flag+" "+s2.flag);
Now the strange behaviour (im very new with spring):
If i declare #Transactional the method "useController()", the output is: true true
If i move #Transactional from TestService to TestControllerImp, and i declare "findAll()" with #Transactional, the output is: false false.
Why i have this behaviour? I know by default #Autowired classes are singletone, but why in the first case the flag still remains true?
Thanks all.
The #Transactional mechanism works on JDK proxies per default and those work on interfaces only.
So if you let TestService be an interface and TestServiceImpl be its implementation, then the above code should work.
e.g. change the class declaration to this:
#Service
public class TestServiceImpl implements TestService {
but the test code must reference the interface, not the class:
// this code remains unchanged
TestService s1=context.getBean(TestService.class);
TestService s2=context.getBean(TestService.class);
Reference:
<tx:advice/> settings (Spring Reference)
Using #Transactional (Spring Reference)
TransactionProxyFactorybean (javadoc)