I am trying to use ServiceA inside #PreAuthorize but it doesn't work
ServiceA
#Service
public class ServiceA {
public void aFun() {
return 1;
}
}
ServiceB
#Service
public class ServiceB {
#Autowire
ServiceA serviceA;
#PreAuthorize("1 == serviceA.aFun()")
public void bFun() {
}
}
but it cause exception that he doesn't found serviceA
According to Sping documentation beans should be referenced with "#" symbol: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions-bean-references
So, try to use:
#PreAuthorize("1 == #serviceA.aFun()")
Related
I am having a problem with mocking an object being injected in to a controller.
I am running an integration test. In my test I have the following setup:
#RunWith(SpringRunner.class)
#SpringBootTest
public class AuthenticationTests {
#TestConfiguration
public class Config {
#Bean
#Primary
public AbstractClient client() {
return new AbstractClient() {
#Override
public ManagedChannel getChannel() {
return new ManagedChannel();
}
};
}
}
}
In the controller being tested, AbstractClient is dependency injected like so:
#Controller
public class MyController {
private ManagedChannel managedChannel;
public MyController(AbstractClient client) {
managedChannel = client.getChannel();
}
}
Whenever I run the test, AbstractClient defined in the #TestConfiguration class is never injected - instead the default one is (which is annotated as #Service).
Can anyone help?
I am using Autowired annotation in a Service class to inject Dao dependency. The code I have written is:
public interface Service {
public List<String> getAllCountries() ;
}
public class ServiceImpl implements Service {
#Autowired
private TestDao testDao;
public TestDao getTestDao() {
return testDao;
}
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
public List<String> getAllCountries() {
// TODO Auto-generated method stub
System.out.println("coming in here:" + testDao);
return testDao.getAllCountries();
}
}
public interface TestDao {
List<String> getAllCountries() ;
}
public class TestDaoImpl implements TestDao{
#Override
public List<String> getAllCountries() {
// TODO Auto-generated method stub
List<String> ls = new ArrayList<>();
ls.add("test1");
return ls;
}
}
And a controller
public class TestController {
public void doSth() {
Service service = new ServiceImpl();
try {
System.out.println("service obj:" + service);
List<String> list = service.getAllCountries();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Beans.xml:
<context:annotation-config/>
<bean id="controller" class="org.test.TestController"/>
<bean id="testDao" class="org.test.TestDaoImpl"/>
And a main class:
ApplicationContext context = new
ClassPathXmlApplicationContext("beans4.xml");
TestController obj= (TestController) context.getBean("controller");
obj.doSth();
But it is throwing NullPointerException in ServiceImpl class. These all classes are in the same package.
Can somebody help me understand what exactly is the issue?
Solution:
public class TestController {
#Autowired
Service service;
//remaining code as it is
}
Your Service class is not managed by Spring . Hence, the dependency is getting injected.
To make your service class managed,
You can use #Service stereo type annotation and do a component scan.
I mean,
package com;
#Service
public class ServiceImpl implement XXXX{
//Your changes
}
and in spring config file:
<context:component-scan base-package="com"/>
I did not see any bean declaration or annotation for the ServiceImpl.
//You canot do it like the following
#controller
class A{
ServiceImpl simp=new ServiceImpl();
}
class ServiceImpl{
#Autowired
Adao adoa;//throws NPE
}
//it should be like this
#Controller
class A{
#autowired
ServiceImpl simp;
}
#Service
class ServiceImpl{
#Autowired
Adao adoa;
}
#Repository
class Adao{
}
Your testDao dependency bean has not been injected and you need the Spring container that you are using annotations and also specify which packages need to be scanned for Autowiring by adding the following:
<context:annotation-config />
<context:component-scan base-package="org.test" />
You can look here on how you can auto wire spring beans.
When i call a service layer from controller,data are being saved in database.
My controller is:
#RestController
#RequestMapping("/api")
public class ApiController {
#Autowired(required = true)
PersonService personService; //This is service layer class
#RequestMapping("/add")
public void add(){
person.setLastName("Rahim");
person.setFirstName("Uddin");
person.setAddress("Dhaka");
person.setCity("Dhaka");
personService.add(person);
}
}
Service layer is:
#Service
#Transactional
public class PersonServiceImpl implements PersonService {
#Autowired
PersonDao personDao;
#Override
public void addPerson(Person person) {
personDao.addPerson(person);
}
}
Till now everything is ok.
But when i call the service layer through another class, null pointer exception is being shown.
At that time:
My controller is:
#RestController
#RequestMapping("/api")
public class ApiController {
#Autowired(required = true)
PersonService personService; //This is service layer class
#RequestMapping("/add")
public void add(){
MiddleClass m=new MiddleClass();
m.create();
}
}
My MiddleClass is:
public class MiddleClass {
#Autowired
PersonService personService;
public void create(){
Person person=new Person();
person.setLastName("Rahim");
person.setFirstName("Uddin");
person.setAddress("Dhaka");
person.setCity("Dhaka");
personService.addPerson(person);//this time same service layer
//is showing null pointer exception here
}
}
WHY?????????? is it for lacking of any annotation in MiddleClass?
You're creating the MiddleClass instance yourself, using new. So Spring has no way to know that it has to inject the service into the MiddleClass instance. It can only inject beans into other beans it creates itself.
So, MiddleClass must be a Spring bean, and it must be autowired into the controller, just like the service is.
I have some Jpa repositories and several Entity class. I need to create a helper object for one of my Entity. Inside that helper I use #Autowire to access the Jpa repositories.
#Entity
class A {
#Transient
Helper helper;
...
}
class Helper {
A a;
#Autowired
CRepository repo;
public Helper(A a) {
this.a = a;
}
}
However, the repo is always null. I've tried using SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this) and #Configurable, but both of them failed. Can anybody provide some hint for me?
BTW, A is instantiated inside a rest controller.
Thanks!.
You can use a BeanUtil class to get any bean that created in Springl
#Service
public class BeanUtil implements ApplicationContextAware {
private static ApplicationContext context;
#Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static <T> T getBean(Class<T> beanClass) {
return context.getBean(beanClass);
}
}
Then you can get the bean.
MyBean obj = BeanUtil.getBean(MyBean.class);
Use constructor injection instead of field injection; this is a best practice all the time anyway. Then it's trivial to inject your A into the controller and pass it as a constructor argument.
#Configurable annotation works fine, but you need to use #EnableSpringConfigured annotation in any configuration class in order to make it work. Read my answer in other stackoverflow post: spring autowiring not working from a non-spring managed class
Entity class should not contain any helpers, even if transient. For a clean design you need to separate concerns, so the entity should not be aware of your business logic. I cannot help you more since I don't know which is the goal of that helper, but here you have other alternatives:
ALTERNATIVE 1 (based on your description seems that helper is an stateful bean, so it is not candidate to be a #Service, which I personally think it should be)
#Controller
public MyController {
#RequestMapping(...)
public void processRequest() {
A a = new A();
...
Helper helper = new Helper(a); // CRepository is successfully autowired
}
}
#Configurable(autowire = Autowire.BY_TYPE)
public class Helper {
A a;
#Autowired
CRepository repo;
}
#Configuration
#EnableSpringConfigured
public Application {
...
}
ALTERNATIVE 2 (make your Helper class stateless so that spring is aware of your beans without the need of extra stuff like #Confgurable/#EnableSpringConfigured)
#Controller
public MyController {
#Autowired Helper helper; // CRepository is correctly autowired
#RequestMapping(...)
public void processRequest() {
A a = new A();
...
helper.doSomething(a);
}
}
#Service
public class Helper {
// A a; remove dependency to A to make it stateless
#Autowired
CRepository repo;
public Helper() {
}
public void doSomething(A a) {
...
repo.save(a);
}
}
You cannot autowire nothing in your Helper class because it isn't managed by Spring.
You can use this approach:
public class HelperManager {
#Autowired
private ApplicationContext context;
public Helper getHelper(A a) {
return context.getBean(Helper.class, a);
}
Configure Helper to be a prototype bean:
#Configuration
public class MyConfiguration {
#Bean
public HelperManager helperManager() {
return new HelperManager();
}
#Bean
#Scope("prototype")
public Helper helper(A a) {
return new Helper(a);
}
}
And finally in your controller:
#Controller
public class MyController {
#Autowired
private HelperManager helperManager;
public someMethodWhereToInstanceYourHelper(A a) {
...
Helper helper = helperManager.getHelper(a);
...
}
}
I am trying to do a one time initialization of my webapp. I need a singleton for the ApplicationListener class, so I set the scope to Singleton, but it is creating multiple instances. This BootStrapper is not defined in any other xml config files.
I know that the default scope is singleton, but had to add #Scope("singleton") because it was not a singleton. Even with this annotation, it still creates multiple instances.
Here is my ApplicationListener.
#Component
#Scope("singleton")
public class BootStrapper implements ApplicationListener<ContextRefreshedEvent> {
Am I missing anything?
To have a callback that is invoked after the bean is initialized, use #PostConstruct.
#Component
public class BootStrapper() {
#PostConstruct
public void doSomething() {
System.out.println("I am initalized!");
}
}
Try like this:
#Configuration
public class TestService
{
private Properties properties;
#Bean
#Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public Properties getAppProperties()
{
try
{
if (properties == null)
{
properties = ServiceUtils.loadProperties();
}
return properties;
}
catch (Exception e)
{
LOGGER.logCaughtException("Exception Occured while loading App Properties.", e);
}
}
}