2013年12月10日星期二

After using spring automated assembly , and how to use the new property inside the assembly not reported null pointer

After using spring automated assembly , and how to use the new assembly inside the property not reported null pointer ;
such as:
public class PeopleMgmtImpl extends BaseMgmt implements PeopleMgmt {

@ Autowired
private PeopleDAO peopleDAO;

public void setPeopleDAO (PeopleDAO peopleDAO) {
this.peopleDAO = peopleDAO;
}
If I new the PeopleMgmtImpl elsewhere , peopleDAO will be reported null pointer ;

There is no good way to solve . Do not say peopleDAO also use new, there is no other way
------ Solution ----------------------------- ---------------
visual ...... no !
------ Solution ---------------------------------------- ----
new out with the new keyword object , jvm will reallocate space and the corresponding pointer , which is not spring entirely on management . So whatever else you do not get spring management , also will report nullpoint wrong
------ Solution -------------------- ------------------------
since the spring no longer with the new . Where needed PeopleMgmtImpl injected through spring.
------ Solution ---------------------------------------- ----
this case, the project will be to write a SpringUtil 's . Direct access , and then assigned to the DAO .
For example, some items are encapsulated within the injection , that is encapsulated packet fixed scan. . . .
At this time there must be a such a Util .

usually have a listener to get ApplicationContext by ServletContextEvent
then later you can get bean from the inside.
------ Solution ---------------------------------------- ----
If you did not use it directly to new spring container, returns a null pointer , if you want to get only ApplicationContext ac = new Xml ..... ("bean.xml") ; this approach it. . . Of course , we should also PeopleMgmtImpl a container management , and nothing else way
------ Solution --------------------- -----------------------
own reading configuration files. Resolution.
------ For reference only -------------------------------------- -
not new it. Automatically injected !
------ For reference only -------------------------------------- -
not new, directly injected into it.
------ For reference only -------------------------------------- -
not use new, the destruction of Spring managed objects when instantiated . That we can not directly instantiate his object, but we can learn from them to read out the configuration .

没有评论:

发表评论