当前位置: 首页 > 知识库问答 >
问题:

运行时创建 Bean 时出错。JAR 文件

应向晨
2023-03-14

我有一个带有Spring Boot的Java应用程序,用MySQL数据库和Spring Security性构建。该应用程序的目的是连接到SOAP API,并使用Thymleaf的前端与数据交互。

当使用IntelliJ IDEA在本地主机上运行该项目时,该项目工作正常,并且具有所有SOAP连接。当运行<code>mvn clean包</code>时,fat jar成功构建。我想将jar文件部署到托管服务器。

感谢任何帮助。

但是当使用<code>java-jar运行它时

错误

        2021-10-02 12:47:58.149  WARN 32908 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: o
        rg.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authController' defined in URL [jar:file:/C:/Users/amerm/Downloads/Compressed/project_name/target/project_name-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/project_name/PersonDetailsClient/Controller/AuthController.class]: Unsatisfied dependency expressed through constr
        uctor parameter 2; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personDetailsServiceImpl' defined in URL [jar:fi
        le:/C:/Users/amerm/Downloads/Compressed/project_name/target/project_name-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/com/project_name/PersonDetailsClient/Service/ServiceImpl/PersonDetai
        lsServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean
        with name 'personDetailsClient' defined in class path resource [com/project_name/PersonDetailsClient/Config/PersonDetailsClientConfiguration.class]: Bean instantiation via fact
        ory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.project.name.PersonDetailsClient.PersonDetailsClient]: Fa
        ctory method 'personDetailsClient' threw exception; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.ws.soap.saaj.Sa
        ajSoapMessageFactory]: Unresolvable class definition; nested exception is java.lang.NoClassDefFoundError: javax/xml/soap/SOAPException

身份验证控制器

@Controller
@RequestMapping(value = "/")
public class AuthController {

    @Autowired
    public AuthController(UserService userService, RoleDao roleDao, IPersonDetailsService personDetailsService) {
        this.personDetailsService = personDetailsService;
        this.userService = userService;
        this.roleDao = roleDao;
    }

    private IPersonDetailsService personDetailsService;

    private UserService userService;

    private RoleDao roleDao;

    @RequestMapping
    public String home(){
        return "redirect:/index";
    }

    @RequestMapping("index")
    public String index(){
        return "index";
    }

    @RequestMapping(value = "signup", method = RequestMethod.GET)
    public String signup(Model model) {
        PersonDetails personDetails = new PersonDetails();

        model.addAttribute("user", personDetails);

        return "signup";
    }

    @RequestMapping(value = "signup", method = RequestMethod.POST)
    public String signupPost(@ModelAttribute("user")  PersonDetails newPersonDetails, Model model) throws DatatypeConfigurationException {

        if (userService.checkUserExists(newPersonDetails.getUsername(), newPersonDetails.getEmailAddress())) {

            if (userService.checkEmailExists(newPersonDetails.getEmailAddress())) {
                model.addAttribute("emailExists", true);
            }

            if (userService.checkUsernameExists(newPersonDetails.getUsername())) {
                model.addAttribute("usernameExists", true);
            }

            return "signup";
        } else {
            Set<UserRole> userRoles = new HashSet<>();
            userRoles.add(new UserRole(newPersonDetails, roleDao.findByName("ROLE_USER")));

            String personId = personDetailsService.findPersonByVariant1(newPersonDetails.getLastName(), newPersonDetails.getInitials(), newPersonDetails.getDateOfBirth()).getPersonDetails().getPersonId();
            userService.createUser(newPersonDetails, userRoles);
            PersonDetails user = userService.findByUsername(newPersonDetails.getUsername());
            user.setPersonId(personId);
            userService.saveUser(user);
            if (personId == null){
                String personIdCreated = personDetailsService.createPersonByVariant1(newPersonDetails.getLastName(), newPersonDetails.getInitials(), newPersonDetails.getDateOfBirth()).getPersonDetails().getPersonId();
                userService.createUser(newPersonDetails, userRoles);
                PersonDetails person = userService.findByUsername(newPersonDetails.getUsername());
                person.setPersonId(personIdCreated);
                userService.saveUser(newPersonDetails);
            }

            return "redirect:/";
        }
    }
}

人员详情服务I mpl

@Service
public class PersonDetailsServiceImpl implements IPersonDetailsService {

    final String PersonDetailsEndPoint = "ENDPOINT_URL";
    final String userId = "NUM";
    final String organisationToken = "NUM";

    PersonDetailsClient personDetailsClient;

    @Autowired
    public PersonDetailsServiceImpl(PersonDetailsClient personDetailsClient) {
        this.personDetailsClient = personDetailsClient;
    }

    @Override
    public GetPersonDetailsResponse findPersonByVariant1(String lastName, String initials, Date dateOfBirth) throws DatatypeConfigurationException {

        ObjectFactory objectFactory = new ObjectFactory();
        GetPersonDetailsRequest.PersonDetails person = objectFactory.createGetPersonDetailsRequestPersonDetails();
        person.setLastName(lastName);
        person.setInitials(initials);
        GregorianCalendar bgc = new GregorianCalendar();
        bgc.setTime(dateOfBirth);
        XMLGregorianCalendar bDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(bgc);
        person.setDateOfBirth(bDate);

        GetPersonDetailsRequest getPersonDetailsRequest = objectFactory.createGetPersonDetailsRequest();
        getPersonDetailsRequest.setPersonDetails(person);

        GetPersonDetailsResponse getPersonDetailsResponse = personDetailsClient.getPersonDetailsResponse(PersonDetailsEndPoint, getPersonDetailsRequest, new TokenHeaderRequestCallback(userId, organisationToken));

        return getPersonDetailsResponse;
    }

    @Override
    public CreatePersonDetailsResponse createPersonByVariant1(String lastName, String initials, Date dateOfBirth) throws DatatypeConfigurationException {

        try {
            ObjectFactory objectFactory = new ObjectFactory();
            CreatePersonDetailsRequest.PersonDetails person = objectFactory.createCreatePersonDetailsRequestPersonDetails();
            person.setLastName(lastName);
            person.setInitials(initials);
            GregorianCalendar bgc = new GregorianCalendar();
            bgc.setTime(dateOfBirth);
            XMLGregorianCalendar bDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(bgc);
            person.setDateOfBirth(bDate);


            CreatePersonDetailsRequest createPersonDetailsRequest = objectFactory.createCreatePersonDetailsRequest();
            createPersonDetailsRequest.setPersonDetails(person);

            CreatePersonDetailsResponse createPersonDetailsResponse = personDetailsClient.createPersonDetailsResponse(PersonDetailsEndPoint, createPersonDetailsRequest, new TokenHeaderRequestCallback(userId, organisationToken));

            return createPersonDetailsResponse;

        } catch (SoapFaultClientException soapFaultClientException) {
            return null;
        }
    }


    @Override
    public CheckChangesPersonDetailsResponse checkPersonChanges(String personId, String title, String lastName, String initials, String firstName, String infix, Date dateOfBirth, String streetName, String houseNumber, String houseNumberAddition, String postalCode, String residence, String country, String countryOfBirth, String lastNameOfBirth, String infixOfBirth, String nationality, GenderType gender, String insurancePolicy, String policyNumber, String socialSecurityNumber, Date dateOfDeath, String telephone, String emailAddress) throws DatatypeConfigurationException {
...
    }

    @Override
    public UpdatePersonDetailsResponse updatePersonDetails(String personId, String title, String lastName, String initials, String firstName, String infix, Date dateOfBirth, String streetName, String houseNumber, String houseNumberAddition, String postalCode, String residence, String country, String countryOfBirth, String lastNameOfBirth, String infixOfBirth, String nationality, GenderType gender, String insurancePolicy, String policyNumber, String socialSecurityNumber, Date dateOfDeath, String telephone, String emailAddress) throws DatatypeConfigurationException {
...
    }
}

人员详细信息客户端配置

@Configuration
public class PersonDetailsClientConfiguration {

    @Bean
    public Jaxb2Marshaller marshaller() throws Exception {

        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("com.<project_name>.stub");
        marshaller.afterPropertiesSet();

        return marshaller;
    }

    @Bean
    public WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) {

        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        webServiceTemplate.setDefaultUri("ENDPOINT_URL");
        webServiceTemplate.setMarshaller(marshaller);
        webServiceTemplate.setUnmarshaller(marshaller);

        return webServiceTemplate;
    }

    @Bean
    public PersonDetailsClient personDetailsClient(Jaxb2Marshaller marshaller) {

        PersonDetailsClient personDetailsClient = new PersonDetailsClient();
        personDetailsClient.setDefaultUri("ENDPOINT_URL");
        personDetailsClient.setMarshaller(marshaller);
        personDetailsClient.setUnmarshaller(marshaller);

        return personDetailsClient;
    }
}

人物详细信息客户端

public class PersonDetailsClient extends WebServiceGatewaySupport {

    public GetPersonDetailsResponse getPersonDetailsResponse(String personDetailsEndPoint, GetPersonDetailsRequest getPersonDetailsRequest, TokenHeaderRequestCallback tokenHeaderRequestCallback) {

        GetPersonDetailsResponse getPersonDetailsResponse = (GetPersonDetailsResponse) getWebServiceTemplate().marshalSendAndReceive(personDetailsEndPoint, getPersonDetailsRequest, tokenHeaderRequestCallback);

        return getPersonDetailsResponse;
    }

    public CreatePersonDetailsResponse createPersonDetailsResponse (String personDetailsEndPoint, CreatePersonDetailsRequest createPersonDetailsRequest, TokenHeaderRequestCallback tokenHeaderRequestCallback) {

        CreatePersonDetailsResponse createPersonDetailsResponse = (CreatePersonDetailsResponse) getWebServiceTemplate().marshalSendAndReceive(personDetailsEndPoint, createPersonDetailsRequest, tokenHeaderRequestCallback);

        return createPersonDetailsResponse;
    }

    public CheckChangesPersonDetailsResponse checkChangesPersonDetailsResponse (String personDetailsEndPoint, CheckChangesPersonDetailsRequest checkChangesPersonDetailsRequest, TokenHeaderRequestCallback tokenHeaderRequestCallback) {

        CheckChangesPersonDetailsResponse checkChangesPersonDetailsResponse = (CheckChangesPersonDetailsResponse) getWebServiceTemplate().marshalSendAndReceive(personDetailsEndPoint, checkChangesPersonDetailsRequest, tokenHeaderRequestCallback);

        return checkChangesPersonDetailsResponse;
    }

    public UpdatePersonDetailsResponse updatePersonDetailsResponse (String personDetailsEndPoint, UpdatePersonDetailsRequest updatePersonDetailsRequest, TokenHeaderRequestCallback tokenHeaderRequestCallback) {

        UpdatePersonDetailsResponse updatePersonDetailsResponse = (UpdatePersonDetailsResponse) getWebServiceTemplate().marshalSendAndReceive(personDetailsEndPoint, updatePersonDetailsRequest, tokenHeaderRequestCallback);

        return updatePersonDetailsResponse;
    }
}

绒球.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.<group_name></groupId>
    <artifactId><artifactId></artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name><Project_name></name>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>

    </dependencies>

    <distributionManagement>
        <snapshotRepository>
            <uniqueVersion>false</uniqueVersion>
            <id>snapshot</id>
            <name>Carewebport Snapshots</name>
            <url>https://repo.maven.apache.org/maven2</url>
            <layout>default</layout>
        </snapshotRepository>
    </distributionManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.14.0</version>
                <configuration>
                    <schemaLanguage>WSDL</schemaLanguage>
                    <generatePackage>com.<project_name>.stub</generatePackage>
                    <schemas>
                        <schema>
                            <url>URL.WSDL</url>
                        </schema>
                    </schemas>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

共有2个答案

牟星火
2023-03-14

1.在sts/eclipse中键入ctrl t并键入类名。如果找到了,那么记下它来自的jar名称,比如说x。如果在类路径上没有找到,那么你必须在这个类存在的地方添加依赖项

2 使用任何zip提取器打开在目标文件夹下构建的jar,并在WEB-INF / lib文件夹中检查jar x是否在该x jar下找到javax/xml/soap/SOAPException。

景凌
2023-03-14

这可能是由您在运行时使用的 Java 版本引起的。您必须将 javax ws 包添加为依赖项,因为它不再与较新的 Java 版本捆绑在一起。请参阅 Java 11 软件包 javax.xml.soap 不存在以获取更多信息

 类似资料:
  • 我试图编译一个非常简单的程序,将包含3个用户的简单表保存到http://localhost/phpmyadmin,以清空名为,users ' '的数据库,但它仍然显示异常,您可以看到。 1个异常org.springframework.beans.factory。BeanCreationException:创建在类路径资源[org/springframework/boot/autoconfigure

  • 当我启动Weblogic时(使用jar:hibernate-core-4.3.6.final.jar和hibernate-jpa-2.1-api-1.0.0.final.jar),遇到以下错误信息: 无法自动连接字段:private org.hibernate.sessionFactory com.nscorp.lars.shopleveling.core.dao.impl.Dataloaddao

  • 我在SpringBoot应用程序中创建HighHendRestClient bean时遇到一个错误。我已经做了一个测试'app',在那里我检查了我可以实例化我想要的对象,然后进行我想要的调用,我现在正在做一个新的应用程序的婴儿步骤。 就我所能看到的(我还没有用它做太多...) 当我添加它时(最初我传入了RestClient bean,但现在我临时创建了一个本地对象,以便更清晰) 我得到这个java

  • 我使用的是Spring 3.1.4 服务实现 DAO实现 web.xml

  • 严重:上下文初始化失败org.springframework.beans.factory.BeanCreationException:创建ServletContext资源[/web-inf/mvc-dispatcher-servlet.xml]中定义的名为“org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerM

  • null 这里出了什么问题。