如何在spring中将一个对象注入另一个类对象?

时间:2014-03-05 10:58:44

标签: spring spring-mvc ejb

/*
 * 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;
    }

}

我有User类我正在检查用户的电子邮件和密码,如果它是真的它将返回一些值,它也正常工作。

如何将此用户类值映射到我的普通类我无法在显示器上打印值

请提前给我一个正确的解决方案。

/*
 * 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;
    }

}

1 个答案:

答案 0 :(得分:0)

您的SubUserAccount-Class包含javax.faces-Management和spring context @Component注释的导入。如果我记得正确,javax.faces会创建自己的上下文。如果您减少了类以将其发布到stackoverflow上,则缺少必要的信息。所以我想,你创建了一个新的spring bean,想要调用JSF注入的东西。这不起作用,因为每个容器都创建自己的实例。

如果这是您真正的SubUserAccount-Class,它应该可以工作。

无论如何,我建议更改你的代码,如果SubUserAccount依赖spring bean,它可能会有所帮助。

@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) {

如果执行以下操作,则在请求中创建新的(第二个)Spring上下文。 (所以你创建了3个上下文)

        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        SubUserAccount sua = (SubUserAccount) context.getBean("subUserAccount");