小编M S*_*ach的帖子

当使用 new 运算符创建对象时,如何在 Spring 中注入依赖项?

我有一个CustomerRelation类,它有一个方法getInstance()和实例变量,即

private CustomerCrudService relationshipService = null;
Run Code Online (Sandbox Code Playgroud)

该方法的简要介绍getInstance

 static public CustomerRelation getInstance() {
    if (instance == null) {
      instance = new customerRelation();//line1
     }
 return instance;//line2
   }
Run Code Online (Sandbox Code Playgroud)

现在我将调试器放在第 1 行。执行此行后,我看到创建了包含关系服务对象的实例。我的问题是在使用 new 运算符创建实例时如何注入依赖关系服务?

虽然在 MyProject-SpringConfig.xml 中我可以看到下面的配置,但当我们使用 new 运算符创建对象时,事件仍然如何被 spring 核心容器拦截?是不是因为spring的customclassloader。如果是,我们在哪里指定?

  <bean class="com.its.portfolio.relationship.CustomerRelation" scope="prototype">
    <property name="relationshipService" ref="relationshipService" />
  </bean>
Run Code Online (Sandbox Code Playgroud)

编辑:-这是 CustomerRelation 的代码

    package com.its.relationship;

    import java.util.List;
    import java.util.Map;

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

    /**
     * accessor for customerrelation meta data
     *
     */
    @Configurable
    public class CustomerRelation implements ICustomerRelation {

      static …
Run Code Online (Sandbox Code Playgroud)

java spring dependency-injection jakarta-ee

4
推荐指数
1
解决办法
2779
查看次数

填写含有OnReady内容的tinymce textarea?

这是我的tinymce代码.我正在准备活动中填充内容'Cust Details'的tinymce textarea.但即使在使用tinymce附加文本区域后,tinyMCE.activeEditor也会计算为null

 $(function() {
            appendTinyMCE();
          function appendTinyMCE(){
            tinyMCE.init({

            // General options
            mode : "textareas",
            theme : "advanced",
            plugins : "preview",
            // Theme options
            theme_advanced_buttons1 : "forecolor,backcolor,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,formatselect,fontselect,fontsizeselect,sub,sup,|,bold,italic,underline,strikethrough",
            theme_advanced_buttons2 : "",
            theme_advanced_buttons3 : "",
            width : "640",
            theme_advanced_toolbar_location : "top",
            theme_advanced_toolbar_align : "left",
            theme_advanced_statusbar_location : "bottom",
            theme_advanced_resizing : true

        });}



      alert("tinyMCE.activeEditor"+tinyMCE.activeEditor);// inyMCE.activeEditor is coming as  null. Not getting why
      if(tinyMCE!=null && tinyMCE.activeEditor!=null)
          {
          tinyMCE.activeEditor.setContent('Cust Details');
          }


    });
Run Code Online (Sandbox Code Playgroud)

请告诉我如何在准备好的活动中填充微小的mce文本区域?

javascript jquery wysiwyg tinymce

4
推荐指数
1
解决办法
3999
查看次数

为什么懒惰加载不能在一对一关联中工作?

@Entity
public class Person {
    @Id
    @GeneratedValue
    private int personId;

    @OneToOne(cascade=CascadeType.ALL, mappedBy="person", fetch=FetchType.LAZY)
    private PersonDetail personDetail;

    //getters and setters
}

@Entity
public class PersonDetail {
    @Id
    @GeneratedValue
    private int personDetailId;

    @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.LAZY)
    private Person person;

        //getters and setters

    }
Run Code Online (Sandbox Code Playgroud)

当我做

 Person person1=(Person)session.get(Person.class, 1);
Run Code Online (Sandbox Code Playgroud)

我看到两个问题被解雇了.一个用于获取人员数据,另一个用于获取人员详细数据.

根据我的理解,应该只触发1个查询,用于获取人员数据而非人员详细数据,因为我已经提到了延迟加载.为什么personDetail数据与人员数据一起被提取?

java hibernate lazy-loading

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

何时使用 Table 每个类层次结构而不是每个子类的 Table?

在任何 web 应用程序中,我们都会遇到这样的场景,我们可以使用每个类层次结构的表或每个子类的表。但重要的是要决定哪一种更适合您的用例。我已经有了我的基本理解,哪个更好?

Scenario :- 员工 。永久雇员和合同雇员扩展雇员。存在两种选择:-

Option 1:-每个类层次结构的表,我们在单个表中表示所有字段。关于它的好处是您可以从单个表中获取所有详细信息,并摆脱 Employee 和 Permanent/Contract Employee 之间的连接。因此提高了性能。但是,是的,即使在您认为对于永久雇员不能为空的字段上,您也不能声明非空约束(因为我们在单个表中同时为永久雇员和合同雇员提供服务)。这是缺点。

Option 2:-每个子类的表,其中我们有对父表的外键引用。在此处加入会降低性能。但是,是的,您可以继续使用 no null 之类的约束,这是您在第一个选项中无法做到的。

My take :- 如果您不需要放置约束,请继续针对此类场景使用选项 1,因为摆脱连接对性能 pov 有好处。

想听听它是否有更多方面或我的理解是否正确?

java database-design hibernate web-applications

4
推荐指数
1
解决办法
3469
查看次数

当存在多个构造函数时,使用autowire ="constructor"进行依赖注入?

我有下面的构造函数的文本编辑器类

 public class TextEditor {
       private SpellChecker spellChecker;

       private SpellChecker1 spellChecker1;

       private SpellChecker2 spellChecker2;

     public TextEditor(SpellChecker spellChecker) {
          this.spellChecker = spellChecker;
        }

       public TextEditor(SpellChecker2 spellChecker2) {
              this.spellChecker2 = spellChecker2;
           }

       public TextEditor(SpellChecker spellChecker, SpellChecker1 spellChecker1,SpellChecker2 spellChecker2) {
              this.spellChecker = spellChecker;
              this.spellChecker1 = spellChecker1;
              this.spellChecker2 = spellChecker2;
           }

       public TextEditor(SpellChecker spellChecker, SpellChecker1 spellChecker1) {
              this.spellChecker = spellChecker;
              this.spellChecker1 = spellChecker1;
           }
        }
Run Code Online (Sandbox Code Playgroud)

在春天豆我有

<bean id="textEditor" class="com.TextEditor" autowire="constructor">
</bean>
Run Code Online (Sandbox Code Playgroud)

我观察到的是具有两个参数的构造函数被一致地调用.是随机的吗?不应该抛出异常becoz它不知道需要调用哪个构造函数?

java spring

4
推荐指数
1
解决办法
2004
查看次数

在这个例子中如何使用hashamp中的Mutable键?

  HashMap hm = new HashMap();
  StringBuilder sb = new StringBuilder("test");
  hm.put(sb, "second");
  // above code gets hacode for sb and places it corresponding bucket

  sb.append("123");// with appending of "123", sb hascode will change

  System.out.println("element is is" + hm.get(sb));// print "second"
Run Code Online (Sandbox Code Playgroud)

现在hm.get(sb)应该根据新的哈希码搜索存储桶中的密钥.所以它不应该在该桶下获得任何对象并且应该为null.那为什么它会变得"第二"呢?

java hashmap immutability

4
推荐指数
1
解决办法
60
查看次数

如何获取链表中的Node组件?

学习了如何反转链表。该链接非常具有描述性且清晰。但不知何故,我不知道如何获取需要传递给下面的方法的 Node 组件。我在LinkedList下没有找到任何 获取Node的方法。抱歉问了愚蠢的问题,但在网络上的任何地方,都假设我们有 Node 组件,然后程序从那里开始

public void recursiveReverse(Node currentNode ){
    ...
}
Run Code Online (Sandbox Code Playgroud)

java linked-list data-structures

4
推荐指数
1
解决办法
4818
查看次数

字符串引用?

我们制作以下String对象?

String str1 = new String("ABC");
String str2 = new String("ABC");
String str3 = "ABC";
String str4 = "ABC";
Run Code Online (Sandbox Code Playgroud)

以上两个问题:

  1. system.out.println("valof str1 "+str1 );- 它打印str1为ABC但是当我们比较时if(str1==str2),它会比较字符串对象的参考.jvm如何达到差异?

  2. str1具有不同的充参考str2str3,但str3str4具有相同的引用,这样做的JVM检查,如果我们要以平等的运营商(而不是新)已经存在(如果它存在,它不会创建新的对象只是指定相同refernce新变量来建立一个字符串ie str4)但是在新操作员的情况下它不会做这个验证?

java string

3
推荐指数
1
解决办法
351
查看次数

类定义中的聚合和组合表示?

聚合是整体/部分关系。如果整体不存在,但一部分仍然存在

但在组成上如果不再存在全部,而是部分不再存在

例如,一所大学拥有多个系(例如化学系),每个系都有许多教授。如果大学关闭,这些系将不再存在,但这些系中的教授将继续存在。因此,大学可以看作是系的组成,而系则是教授的集合体。

我在这里的问题是,我们如何在Java中实际定义大学,系和教授的类定义,该类定义还描述了上述聚合和组合行为?

uml

3
推荐指数
1
解决办法
2850
查看次数

Spring中的PROPAGATION_REQUIRED事务属性?

http://www.vermatech.com/code/SpringTransactionExamples.html上的第一个案例研究中,程序正在调用两个方法,即

testModel.deleteAllCountries();
testModel.initializeCountries();
Run Code Online (Sandbox Code Playgroud)

其中initializeCountries抛出运行时异常.对于这两种方法,事务定义属性为PROPAGATION_REQUIRED.仍然会在deleteAllCountries方法下提交事务,但会回滚initializeCountries下的事务(根据同一案例研究中给出的日志).

根据PROPAGATION_REQUIRED定义,它支持当前事务; 如果不存在则创建一个新的.所以我的问题是在initializeCountries方法下的事务应该支持deleteAllCountries方法下的事务.我的意思是这两种方法都应该被视为单一交易.根据我的理解,还是应该提交或回滚完整的事务?不知道日志是如何分别处理它们的.

java spring java-ee spring-transactions

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