当前位置: 动力学知识库 > 问答 > 编程问答 >

How to inject one object to another class object in spring?

问题描述:

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package com.fbt.corp.web.security;

import com.fbt.corp.ejb.user.business.LoginAuth;

import com.fbt.corp.ejb.user.businesslogic.LoginAuthImpl;

import com.fbt.corp.entity.user.User;

import com.fbt.corp.web.subuser.SubUserAccount;

import com.fbt.corpdap.ejb.EjbUtil;

import com.vigneshb.test.LoginBean;

import com.zahir.test.LoginManager;

import javax.jms.Session;

import javax.naming.Context;

import javax.naming.NamingException;

import javax.servlet.ServletContext;

import javax.servlet.http.HttpSession;

import org.jboss.com.sun.corba.se.spi.presentation.rmi.StubAdapter;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.ModelAttribute;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.context.support.WebApplicationContextUtils;

import org.springframework.web.portlet.ModelAndView;

import sun.security.pkcs11.wrapper.Functions;

/**

*

* @author VigneshB

*/

@Controller

public class LogAuth {

@RequestMapping(value = "/login", method = RequestMethod.GET)

public String myLogin(@RequestParam("email") String email, @RequestParam("pass") String pass, ModelMap map, HttpSession session) {

ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

SubUserAccount sua = (SubUserAccount) context.getBean("subUserAccount");

LoginAuth login = doLookup();

User user;

user = null;

SubUserAccount su = new SubUserAccount();

user = login.logmein(email, pass);

// LoginManager lm=(LoginManager) context.getBean("loginManager");

//

// System.out.println("my status--->"+lm.logtest(email, pass).getUser_type());

// LoginBean lb = new LoginBean(login, pass);

// User user = lb.getUser();

if (user == null) {

return "fail";

} else {

sua.setMobile(user.getMobile());

sua.getMobile();

sua.setName(user.getName());

return "success";

}

// return lm.logtest(email, pass).getUser_type();

}

private static LoginAuth doLookup() {

Context context = null;

LoginAuth bean = null;

try {

// 1. Obtaining Context

context = EjbUtil.getInitialContext();

// 2. Generate JNDI Lookup name

String lookupName = getLookupName();

// 3. Lookup and cast

bean = (LoginAuth) context.lookup(lookupName);

} catch (NamingException e) {

e.printStackTrace();

}

return bean;

}

private static String getLookupName() {

/*

The app name is the EAR name of the deployed EJB without .ear suffix.

Since we haven't deployed the application as a .ear,

the app name for us will be an empty string

*/

String appName = "";

/* The module name is the JAR name of the deployed EJB

without the .jar suffix.

*/

String moduleName = "CorpEJB";

/*AS7 allows each deployment to have an (optional) distinct name.

This can be an empty string if distinct name is not specified.

*/

String distinctName = "";

// The EJB bean implementation class name

String beanName = LoginAuthImpl.class.getSimpleName();

// Fully qualified remote interface name

final String interfaceName = LoginAuth.class.getName();

// Create a look up string name

String name = "ejb:" + appName + "/" + moduleName + "/"

+ distinctName + "/" + beanName + "!" + interfaceName;

return name;

}

}

i have the User class i am checking the user email and password if its true it will return some value it is also working fine.

how to map this user class value to my normal class i am unable to print the value on display

please give me theproper solution for this thanks in advance.

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package com.fbt.corp.web.subuser;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.SessionScoped;

import javax.inject.Scope;

import org.springframework.stereotype.Component;

/**

*

* @author VigneshB

*/

public class SubUserAccount {

String name;

String mobile;

public SubUserAccount() {

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getMobile() {

System.out.println("this is the current value getter ------>"+mobile);

return mobile;

}

public void setMobile(String mobile) {

System.out.println("this is the current value ------>"+mobile);

this.mobile = mobile;

}

}

网友答案:

Your SubUserAccount-Class contains imports for javax.faces-Management and spring context @Component annotation. If i remeber correctly, javax.faces creates its own context. If you reduced your class to post it on stackoverflow, necessery information is missing. So I think, you created a new spring bean and want to call something which is injected by JSF. This wont work, because each container creates its own instance.

If this is your real SubUserAccount-Class, it should work.

Anyway, I would suggest to change your code, and may it helps if SubUserAccount has dependend spring beans.

@Controller
public class LogAuth {
    @Autowired
    private SubUserAccount sua

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String myLogin(@RequestParam("email") String email, @RequestParam("pass") String pass, ModelMap map, HttpSession session) {

If you do the following, your create a new (second) Spring context inside your request. (so you created 3 contexts)

        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        SubUserAccount sua = (SubUserAccount) context.getBean("subUserAccount");
分享给朋友:
您可能感兴趣的文章:
随机阅读: