小编Dan*_*own的帖子

为什么我可以使用命令设计模式,而我可以轻松调用所需的方法?

我正在研究命令设计模式,我对使用它的方式很困惑.我的示例与用于打开和关闭灯的远程控制类有关.

我为什么不应该使用合闸合闸()/关机灯类,而不是单独的类和方法,最终调用合闸合闸/关机方法()方法?

我知道我的例子很简单,但这就是重点.我无法在Internet上的任何地方找到任何复杂的问题来查看命令设计模式的确切用法.

如果你知道,你解决了可以使用这种设计模式请与我共享来解决任何复杂的现实世界的问题.它帮助我和这篇文章的未来读者更好地理解这种设计模式的用法.谢谢

//Command
public interface Command {
  public void execute();
}

//Concrete Command
public class LightOnCommand implements Command {

  //Reference to the light
  Light light;

  public LightOnCommand(Light light) {
    this.light = light;
  }

  public void execute() {
    light.switchOn();        //Explicit call of selected class's method
  }
}

//Concrete Command
public class LightOffCommand implements Command {

  //Reference to the light
  Light light;

  public LightOffCommand(Light light) {
    this.light = light;
  }

  public void execute() {
    light.switchOff();
  }
} …
Run Code Online (Sandbox Code Playgroud)

java oop design-patterns command-pattern

45
推荐指数
5
解决办法
1万
查看次数

如何配置Spring-Security以访问数据库中的用户详细信息?

我对SpringSecurity感到困惑.有很多方法可以实现一个简单的事情,我把它们混合在一起.

我的代码如下,但它抛出异常.如果我删除UserDetailsService相关代码,应用程序运行,我可以登录in-memory用户.如下所示,我将配置转换为基于XML,但用户无法登录.

org.springframework.beans.factory.BeanCreationException: Error creating bean 
with name 'securityConfig': Injection of autowired dependencies failed; nested 
exception is org.springframework.beans.factory.BeanCreationException: Could 
not autowire field:  
org.springframework.security.core.userdetails.UserDetailsService 
com.myproj.config.SecurityConfig.userDetailsService; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying 
bean of type    
[org.springframework.security.core.userdetails.UserDetailsService] found for 
dependency: expected at least 1 bean which qualifies as autowire candidate for 
this dependency. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true),  
@org.springframework.beans.factory.annotation.Qualifier(value=userDetailsService)}

Caused by: org.springframework.beans.factory.BeanCreationException: Could not 
autowire field 

org.springframework.security.core.userdetails.UserDetailsService 
com.myproj.config.SecurityConfig.userDetailsService; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 
[org.springframework.security.core.userdetails.UserDetailsService] 
found for dependency: …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate spring-mvc spring-security

29
推荐指数
3
解决办法
4万
查看次数

如何建立基于非关键字段的关系?

我有两个实体如下,当我尝试将项目添加到我的汽车表时,它显示以下错误消息;因此,它不允许我有多个汽车进行'自动'传输.

错误:

 #1062 - Duplicate entry 'Auto' for key 'UK_bca5dfkfd4fjdhfh4ddirfhdhesr' 
Run Code Online (Sandbox Code Playgroud)

实体:

汽车

@Entity
public class Car  implements java.io.Serializable {


    @Id
    @GeneratedValue
    long id;
    @Column(name="transmission", nullable = false)
    String transmission;
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "car")
    Set<CarFactory> factories;
    ...
}
Run Code Online (Sandbox Code Playgroud)

汽车表的样本值:

10 Auto
12 Auto
43 Manual
54 Manual
65 Normal
68 Standard
90 Normal
99 NoGear
Run Code Online (Sandbox Code Playgroud)

CarFactory

@Entity
public class CarFactory implements java.io.Serializable {

   @Id
   @JoinColumn(name="transmission",referencedColumnName = "transmission")
   @ManyToOne
   Car car;

   @Id
   @JoinColumn(name="factory_id", referencedColumnName= "id")
   @ManyToOne
   Factory factory;

   ... …
Run Code Online (Sandbox Code Playgroud)

java orm hibernate jpa hibernate-mapping

21
推荐指数
2
解决办法
931
查看次数

使用restTemplate时需要忽略证书

我正在尝试向以下地址发送请求.证书无效,我想忽略它.我写下面根据我的研究码1,2,但我无法完成它.我使用的是Java 1.7,

https://api.stubhubsandbox.com/search/catalog/events/v3
Run Code Online (Sandbox Code Playgroud)

private static final TrustManager[] UNQUESTIONING_TRUST_MANAGER = new TrustManager[]{
    new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers(){
            return null;
        }
        public void checkClientTrusted( X509Certificate[] certs, String authType ){}
        public void checkServerTrusted( X509Certificate[] certs, String authType ){}
        public void checkClientTrusted(
                java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
            // TODO Auto-generated method stub

        }
        public void checkServerTrusted(
                java.security.cert.X509Certificate[] arg0, String arg1)
                throws CertificateException {
            // TODO Auto-generated method stub

        }
    }
};

public static void main(String[] args) …
Run Code Online (Sandbox Code Playgroud)

java ssl spring-mvc resttemplate

20
推荐指数
4
解决办法
4万
查看次数

如何在谷歌地图上显示项目类似于谷歌显示其结果的方式

我需要显示一个带有多个标记的地图,我发现这个问题有我想要的但问题是我需要显示它旁边的每个项目的标记.

<c:forEach var="product" items="products">
   ${product.name}
</c:forEach>
Run Code Online (Sandbox Code Playgroud)

我也检查了这个问题的答案,但没有多大帮助.

谷歌地图代码

var pinColor = "FE7569";
var marker, i;

var address=[];
address[0] = "New york";
address[1] = "las vegas";
address[2] = "san francisco";
address[3] = "chicago";

// Set default map center location
var latlng = new google.maps.LatLng(latcenter,longcenter);

// Create pinShadow for each marker 
var pinShadow = new google.maps.MarkerImage("http://chart.apis.google.com        /chart?chst=d_map_pin_shadow",
    new google.maps.Size(40, 37),
    new google.maps.Point(0, 0),
    new google.maps.Point(12, 35));

// Function to create the dynamic marker
function pinImage(i){
    return image = …
Run Code Online (Sandbox Code Playgroud)

javascript jquery jsp google-maps google-maps-api-3

14
推荐指数
1
解决办法
1463
查看次数

如何使用类自定义来解决文件生成冲突

我试图使用Maven生成供Spring框架使用的JAXB文件,但Maven显示以下错误:

我知道它无法生成带有名称的文件,但我不知道如何解决这个问题.到目前为止,我访问了以下链接.1,2,3

org.xml.sax.SAXParseException; systemId: http://www5v80.elsyarres.net/service.asmx?wsdl; lineNumber: 5; columnNumber: 39; A class/interface with the same name "hello.wsdl.SearchFlights" is already in use. Use a class customization to resolve this conflict.
....
org.xml.sax.SAXParseException; systemId: http://www5v80.elsyarres.net/service.asmx?wsdl; lineNumber: 12; columnNumber: 43; (Relevant to above error) another "SearchFlights" is generated from here.
....
org.xml.sax.SAXParseException; systemId: http://www5v80.elsyarres.net/service.asmx?wsdl; lineNumber: 371; columnNumber: 42; A class/interface with the same name "hello.wsdl.GetFlightDetails" is already in use. Use a class customization to resolve this conflict.
....
Run Code Online (Sandbox Code Playgroud)

Maven插件 …

java spring wsdl jaxb maven

14
推荐指数
4
解决办法
3万
查看次数

如何在Spring中构建SOAP客户端?

我能够使用Web服务发送请求javax.xml.soap.*,我想隐藏代码使用webServiceTemplate.

  • 我正在努力创建请求和结果对象.(我发现的样本与xml而非SOAP有关)
  • 我也想知道使用webServiceTemplate 结束是否有任何好处 java.xml.soap.如果我不能正确地做到这一点?鉴于我需要连接到20个Web服务.

它唯一的服务findEvents如下:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://ticketmaster.productserve.com/v2/soap.php" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
   <soapenv:Header/>
   <soapenv:Body>
      <soap:findEvents soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <request xsi:type="soap:Request">
            <!--You may enter the following 7 items in any order-->
            <apiKey xsi:type="xsd:string">?</apiKey>
            <country xsi:type="xsd:string">?</country>
            <resultsPerPage xsi:type="xsd:int">?</resultsPerPage>
            <currentPage xsi:type="xsd:int">?</currentPage>
            <sort xsi:type="soap:Request_Sort">
               <!--You may enter the following 2 items in any order-->
               <field xsi:type="xsd:string">?</field>
               <order xsi:type="xsd:string">?</order>
            </sort>
            <filters xsi:type="soap:ArrayOfRequest_Filter" soapenc:arrayType="soap:Request_Filter[]"/>
            <updatedSince xsi:type="xsd:string">?</updatedSince>
         </request>
      </soap:findEvents>
   </soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)

我的代码如下:

try {
    SOAPConnectionFactory soapConnectionFactory =
            SOAPConnectionFactory.newInstance();
    SOAPConnection …
Run Code Online (Sandbox Code Playgroud)

java spring web-services spring-ws webservicetemplate

12
推荐指数
1
解决办法
8185
查看次数

无法使用JAXB解析XML消息org.springframework.oxm.UnmarshallingFailureException

我使用以下代码来解析soap响应,但我收到UnmarshallingFailureException,我将@XmlSeeAlso更改为@XMLRootElement但问题仍然存在.WSDL就在这里.

  Caused by: javax.xml.bind.UnmarshalException: unexpected element 
       (uri:"ElsyArres.API", local:"SearchFlightsResponse"). Expected elements are 
       <{ElsyArres.API}Inbound>,<{ElsyArres.API}Leg>,<{ElsyArres.API}Legs>,
       <{ElsyArres.API}Outbound>,<{ElsyArres.API}Request>,<{ElsyArres.API}Response>,
       <{ElsyArres.API}SearchFlights>,<{ElsyArres.API}SoapMessage>
Run Code Online (Sandbox Code Playgroud)

   @XmlRootElement(name = "SoapMessage")
   @XmlAccessorType(XmlAccessType.FIELD)
   public class WegoloSoapMessageResponse {
       @XmlElement(name = "Username")
       private String username;
       @XmlElement(name = "Password")
       private String password;
       @XmlElement(name = "LanguageCode")
       private String languageCode;
       @XmlElement(name = "ErrorMessage")
       private String errorMessage;
       @XmlElement(name = "ErrorCode")
       private int errorCode;
       @XmlElement(name = "AppVersion")
       private String appVersion;
       @XmlElement(name = "Request")
       private Request request;
       @XmlElement(name = "Response")
       private Response response;

       getters and setters


   @XmlRootElement(name = "Request")
   @XmlAccessorType(XmlAccessType.FIELD) …
Run Code Online (Sandbox Code Playgroud)

java xml spring jaxb

12
推荐指数
1
解决办法
504
查看次数

如何使用Hibernate定义友谊关系?

我需要有"FriendRequest"和"ListOfFriends"功能.类似于facebook,它显示收到的朋友请求数和已批准的朋友数.

通过"FriendRequest",我的意思是拥有用户收到的朋友请求列表.

通过"ListOfFriends",我的意思是拥有用户的朋友列表.

为了实现这一点,我定义了以下类,但是当我尝试使用其用户名检索用户时,将抛出"Stackoverflow"异常消息.它似乎进入了一个无限循环.

当我从我的toString方法中删除"FriendRequests"和"Friends"时,它会停止抛出异常.

这个问题类似于我要实现的问题但不同之处在于我不知道要拥有单独的Credential类.

会员

@Entity
public class Member implements Serializable {
    @Id
    @GeneratedValue
    private long id;
    @Column(name = "username", nullable = false, unique = true)
    private String username;
    @Column(nullable = false)
    private String fname;
    @Column(nullable = false)
    private String lname;
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "requester")
    private Set<Friendship> friendRequests = new HashSet<Friendship>();
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "friend")
    private Set<Friendship> friends = new HashSet<Friendship>();

    getters and setters

    @Override
    public String toString() {
       return …
Run Code Online (Sandbox Code Playgroud)

java orm hibernate hibernate-mapping

11
推荐指数
1
解决办法
1196
查看次数

为什么堆内存使用量和加载类的数量不断增加?

我正在使用JVM Explorer- 链接到JVM资源管理器,以分析我的Spring应用程序.我有以下问题.

  • 为什么即使在应用程序启动并且还没有收到任何请求后,"使用堆内存"仍在不断增加?(图1)

  • 为什么即使在垃圾收集之后和收到任何请求之前'使用堆内存'仍在不断增加?(图像2)

  • 为什么在垃圾收集之后,通过向应用程序发送一些请求来加载类的数量正在增加?应用程序不应该使用以前的类吗?为什么它只是增加几乎所有东西(堆,加载类的数量)?(图像3)

    应用程序启动后 - 放大图像 在此输入图像描述

    单击"运行垃圾收集器"按钮后.- 放大图像

在此输入图像描述

在完成垃圾收集程序后向应用程序发送一些请求后 - 放大图像

在此输入图像描述

java memory spring profiling spring-mvc

10
推荐指数
1
解决办法
2864
查看次数