1

I have a couple @Value annotations within my Entity Class. I am not sure why but they are both returning null. I also have "ShaPasswordEncoder" Object Autowired, which too is throwing a NullPointer Exception. I have no idea why. Please advise.

@Repository

@Configurable
@Entity
@Table(name="user")
@NamedQueries({...})
public class User implements Serializable{

    @Transient private static final Logger logger = Logger.getLogger(User.class);
    @Transient private static final AppUtil appUtil = new AppUtil();
    @Transient @Value("some value") private String extDir;
    @Transient @Value("100x100") private String imageSize;
    @Transient private static byte[] salt = "FBar".getBytes();
    @Transient @Autowired private ShaPasswordEncoder passwordEncoder;

....
//Default Constructor
    public User(){
        logger.info("TEST IMAGE => "+imageSize);


    }

    public String passwordEncoder(String password) {
        return passwordEncoder.encodePassword(password,salt);
    }
1
  • 1
    You really shouldn't be using simple SHA to hash passwords. Look into something like Blowfish. Commented Jul 29, 2013 at 14:13

4 Answers 4

1

Making a JPA entity as a Spring bean is a bad design. You should keep your entity simple: only getters and setters.

// Only JPA annotations
@Entity
@Table(name="user")
@NamedQueries({...})
public class User {
    // Getters & Setters
}

Then you should delegate the business logic to service classes:

@Service
public class UserService {

    @Autowired
    private ShaPasswordEncoder passwordEncoder;

    @Value("${conf.extDir}")
    private String dir;

   // Some operations ...

   public void createUser(User user) {
       // ...
   }

   public void updateUser(User user) {
       // ...
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Does anyone else agree with this? I always thought business logic should go in the Model, which this case would be @Entity (User Class)?
0

Are you passing valid value expressions? For properties placeholder you can use something like:

@Value("${directory.extDirectory}")

You can also use Spring EL and get all the goodness from it using the #{value} check the docs here

Is also possible to assign a default value in case the property is not found

@Value("${directory.extDirectory:defaultValue}")

1 Comment

I intend to get the values from propertiez file once I managed to get the explicitly defined value to work. I doubt by replacing the above with a reference to a .properties file will solve it.
0

Using Spring annotations on a POJO means you are delegating the creation and the configuration of this bean to the Spring IoC Container !!! The Spring container will supply all the required dependencies.

For example:

@Component
public class MyBean {
    @Value("${data}")
    private String data;

    @Autowired
    private MyService service;

    // ...

}
  • When you try to instantiate a bean using the new operator, you will get null values.

MyBean bean = new MyBean();

  • In order to have a fully-configured bean you MUST get it from the applicationContext. The Spring container will provide all the requested dependencies.

MyBean bean = (MyBean) applicationContext.getBean(MyBean.class);

Or

@Component
public class AnotherBean {

    // You are sure that Spring will create and inject the bean for you.
    @Autowired
    private MyBean bean;
}

2 Comments

You must enable components auto-detecting: <context:component-scan base-package="org.mycompany" />
I have. Besides I am tempted go with your other suggestion i.e. move the business logic to service layer.
0

Although the bean is managed by Spring it is also possible that you make a new bean yourself instead of getting it from spring container.

So below code will make a new user but it is not get from Spring context.

User user = new User()

If you use above code the @value is not applied to your bean.

If you want you must get the User from Spring by @Autowired

public class SampleService{

@Autowired
private User user;

public void Sample(){
user.getExtDir(); //here user.extDir is not null

}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.